Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Facebook C# SDK) Problem getting an access token
    text
    copied!<p>I'm quite new to the Facebook C# SDK (5.0.3) which probablly is the reason for this question. Basically, I'm trying to get the current users profile, email, photo, etc etc. Below you'll find the code to my two pages (MyLogin.aspx and landingpage.aspx). I use web forms buy the way. </p> <p>The first page displays a login button, and then redirects to the landingpage. See my comments in the code for further information. I get various exceptions I don't know how to solve. </p> <p>If you have any guidance that allows me to move forward, I am very grateful for that.</p> <p>So, heres the code ... </p> <p>MyLogin.aspx.cs</p> <pre><code> protected void DoLogin(object sender, EventArgs e) { Response.Redirect(GetFacebookLoginUrl()); } private static string GetFacebookLoginUrl() { try { const string baseUrl = "http://localhost:5000/"; var extendedPermissions = new[] { "offline_access", "publish_stream" }; var oauth = new FacebookOAuthClient { ClientId = FacebookContext.Current.AppId }; //If I use token instead of code the FacebookOAuthResult.TryParse will return false. var parameters = new Dictionary&lt;string, object&gt;{{ "response_type", "code" },{ "display", "page" }}; if (extendedPermissions != null &amp;&amp; extendedPermissions.Length &gt; 0) { var scope = new StringBuilder(); scope.Append(string.Join(",", extendedPermissions)); parameters["scope"] = scope.ToString(); } parameters["redirect_uri"] = String.Format("{0}LandingPage.aspx", baseUrl); var url = oauth.GetLoginUrl(parameters).OriginalString; return url; } catch (Exception) { return string.Empty; } } </code></pre> <p>... and here's the page where it all seems to got to $"!@@%% ... if you know what I mean.</p> <p>Landingpage.aspx.cs</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { var cl = new FacebookOAuthClient(FacebookContext.Current); FacebookOAuthResult result = null; var url = Request.Url.AbsoluteUri; if (!FacebookOAuthResult.TryParse(url, out result)) return; if (result.IsSuccess) { //result.AccessToken is null, that's why I create a new instance of FacebookClient to get hold of the AccessToken. var accessToken1 = result.AccessToken; var app = new FacebookClient(FacebookContext.Current.AppId, FacebookContext.Current.AppSecret); var accessToken2 = app.AccessToken; //I now got an AccessToken but when I call client.Get("me"); // I'll get (OAuthException) An active access token must be used to query information about the current user. //var client = new FacebookClient(accessToken2); //dynamic me = client.Get("me"); //string firstName = me.first_name; //string lastName = me.last_name; //string email = me.email; // So, how do I get an active access token? // Well, I did try using the FacebookOAuthClient object and the method ExchangeCodeForAccessToken (as you can see below). cl.ClientId = FacebookContext.Current.AppId; cl.ClientSecret = FacebookContext.Current.AppSecret; cl.RedirectUri = new UriBuilder("http://localhost:5000/").Uri; var parameters = new Dictionary&lt;string, object&gt; { { "permissions", "offline_access" } }; var x = cl.ExchangeCodeForAccessToken(result.Code, parameters); //However, this now gives me the Exception (OAuthException) Error validating verification code. } else { var errorDescription = result.ErrorDescription; var errorReason = result.ErrorReason; } } </code></pre> <p>Thanx!! </p> <p>// Nicke</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload