Facebook授权访问令牌?

时间:2011-05-06 10:46:42

标签: android facebook facebook-android-sdk

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

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);
}

}

我想知道......一旦用户验证了如何存储访问令牌?在教程中没有看到任何提及它。

3 个答案:

答案 0 :(得分:5)

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();
}

即将其保存在共享偏好

答案 1 :(得分:0)

FYI。 facebook.authorize自Facebook SDK 3以来已弃用

答案 2 :(得分:-1)

尝试这种方式

@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

                        }