locked
Renew Expired Access Token - Facebook

    Question

  • OK, new question:

    Anyone know how I can renew my Facebook Access token without taking users to the dialog again? I am using the Web authentication broker sample to get the initial token, and the following code to detect if the token has expired when attempting a post:

     

    function (errorResponse) {
                    var result = JSON.parse(errorResponse.responseText);
                    var resultString = (result.error.message).toString();
    
                    // if response is that token is expired
                    if (resultString.indexOf("Error validating access token") != -1 ) {

    However I cannot find a way to renew the token without presenting the dialog again. Can anyone help??

    :D

    Sunday, November 25, 2012 10:58 AM

Answers

  • a lot of hacking and referencing the PHP tutorials and I got it! here is the answer for anyone else having trouble:

    function renewToken(clientID, secret, accessToken) {
    
            var facebookURL = "https://graph.facebook.com/oauth/access_token?client_id=" + clientID + "&client_secret=" + secret +"&grant_type=fb_exchange_token&fb_exchange_token=" + accessToken;
    
            WinJS.xhr({
                type: "GET",
                url: facebookURL,
            }).then(callbackToken, callbackTokenAuthError);
    }
    
    function callbackToken(result) {
            var url = result.responseText;
            var param1 = url.split("&");
            var param2 = param1[0].split("=");
            
            localSettings.values["Facebook.AccessToken"] = param2[1];
    
    }
    
    function callbackTokenAuthError(result) {
            console.log("no token");
    }
    

    just pass your app clientID, secret, and the current expired access token into this function. the new access token is

    param2[1];

    in my case I save it to local settings.

    Hope this helps some people that get stuck!


    • Marked as answer by kmrastegar Sunday, November 25, 2012 12:00 PM
    • Edited by kmrastegar Sunday, November 25, 2012 12:02 PM
    Sunday, November 25, 2012 11:59 AM