【发布时间】:2011-03-11 13:23:21
【问题描述】:
我对 Facebook C# SDK (5.0.3) 很陌生,这可能是这个问题的原因。 基本上,我正在尝试获取当前用户的个人资料、电子邮件、照片等。下面是我的两个页面(MyLogin.aspx 和landingpage.aspx)的代码。我使用网络表单购买方式。
第一个页面显示一个登录按钮,然后重定向到登录页面。有关更多信息,请参阅代码中的我的 cmets。我遇到了各种我不知道如何解决的异常。
如果您有任何指导可以让我继续前进,我非常感激。
所以,这里的代码...
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;
}
}
谢谢!!
// 尼克
【问题讨论】:
标签: facebook-c#-sdk