开发者

Facebook authorization access token?

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.


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


FYI. facebook.authorize had deprecated since Facebook SDK 3


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

                        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜