简体   繁体   中英

Exchange short-lived access token for long-lived, not working

I am trying to exchange a short-lived user access token for a ling-lived, but getting an an error (in all.js):

ReferenceError: invalid assignment left-hand side

...TZCOZCkuZBxZAzVUSokiOJbXZAHhESJvuA97qXTpVbVj3P7AZDZD&expires=512326

response.error.message: unknown error

Code:

window.fbAsyncInit = function () {
           FB.init({
               appId: 'xxx', // App ID
               status: true, // check login status
               cookie: true, // enable cookies to allow the server to access the session
               xfbml: true,  // parse XFBML
               oauth: true
           });

           FB.login(function (response) {
                    if (response) {

                    var accessToken = response.authResponse.accessToken;

                    FB.getLoginStatus(function(response) {
                        if (response.status === 'connected') {

                            var accessToken = response.authResponse.accessToken;

                            var OauthParams = {};
                            OauthParams['client_id'] = 'xxx';
                            OauthParams['client_secret'] = 'xxx';
                            OauthParams['grant_type'] = 'fb_exchange_token';
                            OauthParams['fb_exchange_token'] = accessToken;
                            OauthParams['response_type'] = 'token';
                            console.log(accessToken);

                            FB.api('/oauth/access_token', 'post', OauthParams, function(response) {
                                if (!response || response.error) {
                                    console.log(response.error.message);
                                } 
                                else {
                                    console.log(response.accesstoken);
                                }
                            });        
                        }
                    });
                    };

           }, { scope: 'manage_pages' });

       };


}

(function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
}(document));

Anyone has an idea?

Thanks, /M

according to scenario 4 of the documentation you have to send a GET request to

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

maybe the problem is that you are using POST request, but I am not sure

PS I wouldn't recommend to use APP_SECRET on client side

I had a similar issue running tests from a FB Javascript SDK client. The problem on my end was that SDK was tripping handling the access_token in fb_exchange_token . Changing just last three characters in this access token string made the error go away. Of course, this doesn't help solving the problem, but at least this is where the issue was in my case.

Since these calls must be made from a server anyway, I am not going to proceed troubleshooting this further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM