问题
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.
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.
If you have any guidance that allows me to move forward, I am very grateful for that.
So, heres the code ...
MyLogin.aspx.cs
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<string, object>{{ "response_type", "code" },{ "display", "page" }};
if (extendedPermissions != null && extendedPermissions.Length > 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;
}
}
... and here's the page where it all seems to got to $"!@@%% ... if you know what I mean.
Landingpage.aspx.cs
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<string, object> { { "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;
}
}
Thanx!!
// Nicke
回答1:
I think the problem is that you never get a User Access Token you only get a App Access Token. In a standalone app you need to use the oAuth Dialog. The simplest way to do this is using the Javascript SDK.
Facebook C# SDK has a sample that shows you how to do this. You could download the whole sample app (CSASPNETWebsite) as a startingpoint.
回答2:
In both the code request and access token request the redirect_uri must be the same, this will fix the OAuthException in the end. My code is now something like:
...
if (result.IsSuccess)
{
var tokenresult = cl.ExchangeCodeForAccessToken(result.Code, new Dictionary<string, object> { { "redirect_uri", "<your_redirect_uri>" } });
}
...
回答3:
You code is correct for the most part, the only piece you were missing was the "redirect_uri" parameter when calling the "ExchangeCodeForAccessToken" method. This has to match the App URL you set up when you registered your Facebook App. Otherwise, you will get the error you were receiving. You don't need the "permissions" parameter, you have already received that during the prior authorization.
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("redirect_uri", "http://www.yourcallbackurl.com/");
var result = cl.ExchangeCodeForAccessToken(result.code,parameters);
来源:https://stackoverflow.com/questions/5273425/facebook-c-sharp-sdk-problem-getting-an-access-token