在Android上获得长期存在的Facebook Access令牌

时间:2015-08-18 12:25:39

标签: android facebook facebook-graph-api facebook-access-token

这段代码给了我一个短命的访问令牌 -

AccessToken ac = AccessToken.getCurrentAccessToken();

从Facebook文档中,我发现了图形请求端点,用于为长期访问令牌交换短期访问令牌 -

https://graph.facebook.com/oauth/access_token?             
    client_id=APP_ID&
    client_secret=APP_SECRET&
    grant_type=fb_exchange_token&
    fb_exchange_token=EXISTING_ACCESS_TOKEN

在Facebook Graph API资源管理器上使用它时,它给了我一个像这样的JSON -

{
  "og_object": {
    "id": "xxxx027724271xxxx",
    "type": "website",
    "updated_time": "2015-08-13T21:32:57+0000",
    "url": "https://graph.facebook.com/oauth/access_token"
  },
  "share": {
    "comment_count": 1,
    "share_count": 100
  },
  "id": "https://graph.facebook.com/oauth/access_token"
}

但是如何获得新的长期访问令牌?

1 个答案:

答案 0 :(得分:-1)

您必须在代码中使用该ProfileTracker,例如

profileTracker = new ProfileTracker() {

        @Override
        protected void onCurrentProfileChanged(Profile oldProfile,
                Profile currentProfile) {
            if (currentProfile != null && is_in_reg == false) {
                GraphRequest request = GraphRequest.newMeRequest(
                        AccessToken.getCurrentAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object,
                                    GraphResponse response) {
}
                        });

                Bundle parameters = new Bundle();
                parameters.putString("fields",
                        "id,name,email,first_name,last_name,picture.width(150),gender,birthday");
                request.setParameters(parameters);
                request.executeAsync();
            }

            //stopTracking();
        }
    };

    profileTracker.startTracking();