views:

75

answers:

2

So i'm trying to de-serialize the JSON returned from the Graph API OAuth Token call.

The JSON looks like this:

"[{\"access_token\":\"bunchofjsondatablahblah",\"expires\":9999}]"

I'm trying to de-serialize it (using the DataContractJsonSerializer class) into this object:

[DataContract]
internal class FacebookOAuthToken
{
     [DataMember]
     internal string access_token;

     [DataMember]
     internal string expires;
}

Here's how im (trying) to do it:

FacebookOAuthToken token;
using (Stream responseStream = (response.GetReponseStream()))
{
   DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(FacebookOAuthToken));
   token = (FacebookOAuthToken)json.ReadObject(responseStream);
}

This technique is based on this article from MSDN.

However, the properties of token are always null.

Whereas if i do responseStream.ReadToEnd(), it's all fine (returns the above JSON) - which means its not a problem with the actual HTTP request/response, i'm just not deserializing it properly.

What am i doing wrong?

A: 

Okay so it turns out i was using the wrong Graph API URL (so it seems).

This is the problem with the Facebook API Documentation, its all over the place and different threads (google, stack overflow) say different ways how to do things.

I changed to the URL https://graph.facebook.com/oauth/access_token?{0} instead of https://graph.facebook.com/oauth/exchange_sessions?{0} and it now returns a basic string (non-JSON).

So it doesnt need to be serialized at all.

Still, some people might have the same problem as me above, in that case im not sure how to solve.

I'm now using the method defined here.

RPM1984
A: 

If I interpret the JSON string correctly it represents a collection of FacebookOAuthToken items with one single instance in it and not just a single token.

Maybe that is why your deserialization did not work.

Anton
True. Good point. I now have the deserialization working - by using a (sigh) different Facebook Graph API (see my answer below). With this new API, you dont need to deserialize the token, but you do need to deserialize other api calls - which with the above original code, works fine. The cause of my original problem was intermittent FB connection problems that happened today (server was 500'ing)
RPM1984