1

I use Facebook C# SDK v 6 and enabled "Remove Offline Access" on my application settings and after login and get the access token, I am trying to exchange for long lived token(60days one) I am unable to get it as both tokens expiration is with in 24 hrs.

Here is my code

For log in to Facebook

private const string Scope = "publish_stream,manage_pages";
   FacebookClient _fb = new FacebookClient();
   var fbLoginUrl = _fb.GetLoginUrl(
                new
                {
                    client_id = AppId,
                    client_secret = Appsecret,
                    redirect_uri = RedirectUri,
                    response_type = "code",
                    scope = Scope,
                    state = state
                });

To get short lived access token

if (Request.QueryString["code"] != null)
            code = Request.QueryString["code"];
           var result = _fb.Post("oauth/access_token",
                                  new
                                  {
                                      client_id = AppId,
                                      client_secret = Appsecret,
                                      redirect_uri = RedirectUri,
                                      code = code,
                                      scope = Scope,
                                      response_type="token"
                                  });

To get long lived access token

       var  result1 = _fb.Post("oauth/access_token",
                                  new
                                  {
                                      client_id = AppId,
                                      client_secret = Appsecret,
                                      grant_type = "fb_exchange_token",
                                      fb_exchange_token= Session["fb_access_token"] as string
                                  });
  • I think that endpoint wants to be talked to via HTTP __GET__, whereas you are using POST … – CBroe Aug 13 '12 at 14:02
  • Thanks for answering. I tried HTTP GET still the same response. If I remove app permissions and re-add, First time after it prompts permission dialog it is getting expires value (i.e. long lived token). But If I run the same app again it's not getting the expires value. – user1401299 Aug 13 '12 at 18:15
  • So I guess it might be this bug, https://developers.facebook.com/bugs/339375252810525 Have you checked your tokens with the debug tool, what their actual lifetime is? https://developers.facebook.com/tools/debug – CBroe Aug 14 '12 at 08:16
  • Here is the response by using debug tool, I am confused as it said expires to never. App ID: APP ID : Test User ID: : Issued: (22 hours ago) Expires: Never Valid: True Origin: Web – user1401299 Aug 14 '12 at 15:14

1 Answers1

1

This will do.

var result = GetExtendedAccessToken("my_short_lived_old_token");
var extendedToken = result.access_token;

public dynamic GetExtendedAccessToken(string oldToken)
{
    dynamic result = new ExpandoObject();
    try
    {
        var api = new FacebookClient
        {
            AccessToken = oldToken,
            AppId = ClientID,
            AppSecret = ClientSecret
        };
        dynamic parameters = new ExpandoObject();
        parameters.grant_type = "fb_exchange_token";
        parameters.fb_exchange_token = oldToken;
        parameters.client_id = ClientID;
        parameters.client_secret = ClientSecret;
        result = api.Get("oauth/access_token", parameters);
    }
    catch (FacebookOAuthException err)
    {
        result.error = "Error";
        result.message = err.Message;
    }
    catch (Exception err)
    {
        result.error = "Error";
        result.message = err.Message;
    }
    return result;
}
naveen
  • 53,448
  • 46
  • 161
  • 251
  • I get the access token but not expires value. I was getting the access token even with the code I posted earlier but it's not returning the expires value along with it. But If I remove the application, the first time when I make a request it is getting the expires value after that it's only returning access token. – user1401299 Aug 20 '12 at 14:00