Search code examples
c#asp.netfacebookfacebook-c#-sdk

Connecting to new Facebook Client with access token


I finally got the right Facebook access token generated from the code. However now I have another problem. Using that access token to fetch user data. Every time I use the token to fetch data, it returns with an exception !String.IsNullOrEmpty(accessToken).

Full stack Trace:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Diagnostics.Contracts.__ContractsRuntime+ContractException: Precondition failed: !string.IsNullOrEmpty(accessToken)

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ContractException: Precondition failed: !string.IsNullOrEmpty(accessToken)]
   Facebook.Web.FacebookWebClient..ctor(String accessToken) +137
   Facebook_Photo_App._Default.Button1_Click(Object sender, EventArgs e) in Default.aspx.cs:105
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

My code:

To generate the access token on the page load:

 string code;
            code = Request.QueryString["code"];
        //TextBox1.Text = code;

        string token;

        string url = "https://graph.facebook.com/oauth/access_token?client_id=XXX&redirect_uri=http://apps.facebook.com/dutch-vegas/&client_secret=XXX&code=" + code;

        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        token = reader.ReadToEnd();
        string decodedtoken = token;

        TextBox1.Text = decodedtoken;

        decodedtoken = decodedtoken.Replace("access_token=", "");

        int start = decodedtoken.IndexOf("&");
        int count = decodedtoken.Length - decodedtoken.IndexOf("&");
        decodedtoken = decodedtoken.Remove(start, count);

On the button implement the Access token to fetch user data

var client = new FacebookClient(decodedtoken);


        dynamic me = client.Get("me");
        string email = me.email;
        string firstname = me.first_name;
        string lastname = me.last_name;
        string birthday = me.birthday;

        TextBox1.Text = email;

Everything seems to be generating correctly. Even after getting the wrong access token (which was a lot shorter than it is suppose to be) for about a week, I got it sorted. And now I am stuck.

I have also noticed in the C# sdk for Facebook you can create multiple instances for FacebookWebClient, FacebookApp, FacebookClient.

Which one do I use and could this be part of my problem?


Solution

  • There are utility classes in the Facebook SDK that deal with the access token for you. They were a little flaky when I looked at them a few weeks ago though.

    I ended up using the javascript code instead - http://www.patternwebsolutions.com/2011/03/07/facebooksdk-connect-mvc3/