我对Facebook C#SDK(5.0.3)很陌生,这可能是这个问题的原因。 基本上,我正在尝试获取当前用户的个人资料,电子邮件,照片等。下面你会找到我的两个页面的代码(MyLogin.aspx和landingpage.aspx)。我用web表单买方式。
第一页显示登录按钮,然后重定向到目标网页。有关详细信息,请参阅代码中的注释。我得到各种例外,我不知道如何解决。
如果你有任何指导可以让我前进,我非常感激。
所以,继续代码......
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;
}
}
...这里的页面似乎只有$“!@@ %% ...如果你知道我的意思。
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;
}
}
感谢名单!!
// Nicke
答案 0 :(得分:4)
在代码请求和访问令牌请求中,redirect_uri必须相同,这将最终修复OAuthException。我的代码现在是这样的:
...
if (result.IsSuccess)
{
var tokenresult = cl.ExchangeCodeForAccessToken(result.Code, new Dictionary<string, object> { { "redirect_uri", "<your_redirect_uri>" } });
}
...
答案 1 :(得分:2)
我认为问题在于您从未获得用户访问令牌,您只能获得App Access令牌。在独立应用程序中,您需要使用oAuth对话框。最简单的方法是使用Javascript SDK。
Facebook C#SDK有sample,向您展示如何执行此操作。您可以下载整个示例应用程序(CSASPNETWebsite)作为起始点。
答案 2 :(得分:2)
您的代码大部分都是正确的,您在调用“ExchangeCodeForAccessToken”方法时唯一缺少的是“redirect_uri”参数。这必须与您在注册Facebook App时设置的App URL相匹配。否则,您将收到您收到的错误。您不需要“权限”参数,您在事先授权期间已经收到该参数。
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("redirect_uri", "http://www.yourcallbackurl.com/");
var result = cl.ExchangeCodeForAccessToken(result.code,parameters);