简体   繁体   中英

get permanent facebook access token

I have used JSDK to authenticate user for my facebook app. I am getting the access token but it gets expired within 1/2 hours. So how can I get long term Access Token?

The code I have used is:

 var fb = new FacebookClient();
 Dictionary<string,Object> sParams=new Dictionary<string,Object>();
 sParams.Add("client_id",My_App_ID);
 sParams.Add("client_secret",My_App_Secret);
 sParams.Add("grant_type","fb_exchange_token");
 sParams.Add("fb_exchange_token",Short_lived_accessToken);
 dynamic result = fb.Get("oauth/access_token",sParams);
 fb.AccessToken = result.access_token;

but it gives me error Invalid JSON String at line dynamic result = fb.Get("oauth/access_token",sParams);

What wrong am I doing here?

Use this code:

        var client = new FacebookClient(Short_lived_accessToken);

        dynamic result = client.Post("oauth/access_token", new
        {
            client_id = My_App_ID,
            client_secret = My_App_Secret,
            grant_type = "fb_exchange_token",
            fb_exchange_token = Short_lived_accessToken
        });

        Response.Write("Long live access token: [" + result.access_token + "]");

Hope it helps.

The return you will get from the endpoint simply is not JSON, but just plain text in the form

access_token=new_long-lived_access_token&expires=5130106

So you will have to tell your application somehow(?), that the result is not JSON; or you might have to use a different method altogether to make the request, if FacebookClient::Get always expects the answer to be JSON.

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