Facebook 授权访问令牌?

发布于 2024-11-05 07:49:10 字数 1097 浏览 0 评论 0原文

我正在使用教程代码...

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;

public class FacebookSSO extends Activity {

Facebook facebook = new Facebook("ID");

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },

        new DialogListener() {
        @Override
        public void onComplete(Bundle values) {}

        @Override
        public void onFacebookError(FacebookError error) {}

        @Override
        public void onError(DialogError e) {}

        @Override
        public void onCancel() {}
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

}

我想知道...一旦用户通过身份验证,我将如何存储访问令牌?在教程上看不到任何提及。

I'm using the tutorial code...

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.android.*;
import com.facebook.android.Facebook.*;

public class FacebookSSO extends Activity {

Facebook facebook = new Facebook("ID");

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },

        new DialogListener() {
        @Override
        public void onComplete(Bundle values) {}

        @Override
        public void onFacebookError(FacebookError error) {}

        @Override
        public void onError(DialogError e) {}

        @Override
        public void onCancel() {}
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

}

I'm wondering... once the user has authenticated how would I store the access token? couldn't see any mention of it on the tutorial.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煞人兵器 2024-11-12 07:49:10
facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },

    new DialogListener() {
    @Override
    public void onComplete(Bundle values) {}
    String token=facebook.getAccessToken();  //get access token
    save(token);
    @Override
    public void onFacebookError(FacebookError error) {}

    @Override
    public void onError(DialogError e) {}

    @Override
    public void onCancel() {}
});
}

private void save(String token){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("Token", token).commit();
}

即保存在共享首选项中

facebook.authorize(this,new String[] { "offline_access", "publish_stream", "email" },

    new DialogListener() {
    @Override
    public void onComplete(Bundle values) {}
    String token=facebook.getAccessToken();  //get access token
    save(token);
    @Override
    public void onFacebookError(FacebookError error) {}

    @Override
    public void onError(DialogError e) {}

    @Override
    public void onCancel() {}
});
}

private void save(String token){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString("Token", token).commit();
}

i.e save it in sharedpreferences

多像笑话 2024-11-12 07:49:10

供参考。自 Facebook SDK 3 起,facebook.authorize 已弃用

FYI. facebook.authorize had deprecated since Facebook SDK 3

清浅ˋ旧时光 2024-11-12 07:49:10

尝试用这种方法

@Override
                        public void onComplete(Bundle values) {
                            // Function to handle complete event
                            // Edit Preferences and update facebook acess_token
                            SharedPreferences.Editor editor = mPrefs.edit();
                            editor.putString("access_token",
                                    facebook.getAccessToken());
                            editor.putLong("access_expires",
                                    facebook.getAccessExpires());
                            editor.commit();
                        }

                        @Override
                        public void onError(DialogError error) {
                            // Function to handle error

                        }

                        @Override
                        public void onFacebookError(FacebookError fberror) {
                            // Function to handle Facebook errors

                        }

Try in this way

@Override
                        public void onComplete(Bundle values) {
                            // Function to handle complete event
                            // Edit Preferences and update facebook acess_token
                            SharedPreferences.Editor editor = mPrefs.edit();
                            editor.putString("access_token",
                                    facebook.getAccessToken());
                            editor.putLong("access_expires",
                                    facebook.getAccessExpires());
                            editor.commit();
                        }

                        @Override
                        public void onError(DialogError error) {
                            // Function to handle error

                        }

                        @Override
                        public void onFacebookError(FacebookError fberror) {
                            // Function to handle Facebook errors

                        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文