4

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

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

}

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

4

3 回答 3

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

即保存在共享首选项中

于 2011-07-21T10:42:20.510 回答
0

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

于 2013-04-21T16:36:09.997 回答
-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

                        }
于 2014-04-10T06:33:39.807 回答