Console app utilizing Facebook offline_access extended permissions and Facebook C# SDK

StackOverflow https://stackoverflow.com/questions/4458613

  •  10-10-2019
  •  | 
  •  

Question

I'm a refugee from the old Facebook Developer Toolkit porting my app to the newer Facebook C# SDK. I've got the MVC web app side of my solution worked out, but I also have a console application that I run in batch as part of my overall solution. I obtain the offline_access extended permission for all of my users and store the non-expiring session key for later use in my console app.

With the Facebook Developer Toolkit I was able to spin up a Connect Session and REST api using my API Key, API Secret, and the saved user session key and make Facebook api calls.

In the Facebook C# SDK the non-web samples seem to rely on popping up some kind of browser control for interactive user login. That won't work for a console batch application processing users offline.

I've got this far:

string oAuthAccessToken = "{access token}"
var app = new Facebook.FacebookApp(oAuthAccessToken );

// now I can make api calls like this:
dynamic currentPermissionsJson = new ExpandoObject();
currentPermissionsJson = app.Query(string.Format("SELECT publish_stream, offline_access, email from permissions where uid = {0}", {userid}));
var currentPermissions = ((JsonArray)currentPermissionsJson)[0] as IDictionary<string, object>;

and away we go.

I'm just stuck on how to convert my existing stored session keys to Facebook OAuth access tokens. I can see how I can construct POSTs to https://graph.facebook.com/oauth/exchange_sessions with params such as

client_id={my app id}
&client_secret={my app secret}
&sessions={previously stored session keys}

and get the access token back in the response.

But I'm thinking the SDK must offer some method of doing this for me. Or does it?

Was it helpful?

Solution

Unfortunately, we dont have any helpers in the current Facebook C# SDK to exchange the access tokens. I have created an issue and I will try to get it in there shortly. http://facebooksdk.codeplex.com/workitem/5788

For now I wrote up this. Give it a try and let me know how it works:

public class FacebookOAuth
{

    public static IEnumerable<ExchangeSessionResult> ExchangeSessions(string appId, string appSecret, params string[] sessionKeys)
    {
        WebClient client = new WebClient();
        var dict = new Dictionary<string, object>();
        dict.Add("client_id", appId);
        dict.Add("client_secret", appSecret);
        dict.Add("sessions", String.Join(",", sessionKeys));
        string data = dict.ToJsonQueryString();
        string result = client.UploadString("https://graph.facebook.com/oauth/exchange_sessions", data);
        return Newtonsoft.Json.JsonConvert.DeserializeObject<ExchangeSessionResult[]>(result);
    }

}

public class ExchangeSessionResult
{

    [Newtonsoft.Json.JsonProperty("access_token")]
    public string AccessToken { get; set; }

    [Newtonsoft.Json.JsonProperty("expires")]
    public string Expires { get; set; }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top