开发者

How to retrieve the facebook oauth 2.0 access_token

I am trying to get the access_token of the logged in facebook user.

I get something like this.. url followed by the code i am retrieve the code.. 开发者_运维技巧

it says in the guide to exchange it for access_token...

https://graph.facebook.com/oauth/access_token? client_id=XXXXXXXXXXXXXX& redirect_uri=http://www.my-site.com/& client_secret=XXXXXXXXXXXXXXXXXXXXX& code=2.hJFWoMlLf3tLOSos_qNCBg__.3600.1279836000-10000100XXXXXXX|kGwPB4y5K_-ijD9_1CfjSpT-oaY..

How i can exchange it for a access_token using what FB.api or jquery or javascript..

When i plug this url in the address bar.. i am able to see access_token..

I appreciate if somebody can tell me how to retrieve the access_token using javascript or jquery.

Thanks.


According to Facebook, and my personal experiences with the api, the FB.getSession() function is deprecated and should throw an exception if called.

From facebook:

The FB.getAuthResponse method is a replacement for the FB.getSession method which was deprecated after the migration to OAuth 2.0.

Instead you should use the getLoginStatus callback -


FB.getLoginStatus(function(response) {
   var token = response.authResponse.accessToken;
});

//Example structure of response object
var eg =
{
    status: 'connected',
    authResponse: {
        accessToken: '...',
        expiresIn:'...',
        signedRequest:'...',
        userID:'...'
    }
};

source: http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/


If you are using the JavaScript SDK, then after logging in the user, getting the access token in your JavaScript code is as simple as:

FB.getSession().access_token

However, getSession may return null if a user is logged out, so the proper way is to first check for null before accessing the token.

var session = FB.getSession();
if(session != null) { // user is still logged in
    console.log(session.access_token);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜