編碼平臺ASP.NET 4.0 Web窗體上取得成功的着陸頁的accessToken使用Facebook的C#SDK
我有兩個頁面,在這裏是有意義
- 的Login.aspx
- LandingPage.aspx
在Login.aspx當我點擊一個ImageButton時,我用下面的代碼重定向到Facebook網站
protected void FacebookLoginButton_Click(object sender, ImageClickEventArgs e)
{
try
{
Response.Redirect(GetFacebookLoginURL());
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
catch (Exception err)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(err);
}
}
private string GetFacebookLoginURL()
{
try
{
string baseURL = System.Configuration
.ConfigurationManager
.AppSettings["WebsiteURL"]
.ToString();
string[] extendedPermissions = new[] {
"publish_stream",
"offline_access"
};
var oauth = new FacebookOAuthClient {
ClientId = FacebookContext.Current.AppId
};
var parameters = new Dictionary<string, object>{
{ "response_type", "token" },
{ "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);
return oauth.GetLoginUrl(parameters).OriginalString;
}
catch (Exception err)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(err);
return "";
}
}
該部分工作正常。但我對如何訪問LandingPage上的用戶信息無能爲力,這是我的redirect_uri。已經嘗試過。
FacebookOAuthClient cl = new FacebookOAuthClient(FacebookContext.Current);
FacebookOAuthResult result = null;
string url = Request.Url.AbsoluteUri;
// verify that there is a code in the url
if (FacebookOAuthResult.TryParse(url, out result))
{
if (result.IsSuccess)
{
var accesstoken = result.AccessToken;
}
else
{
var errorDescription = result.ErrorDescription;
var errorReason = result.ErrorReason;
}
}
但我懷疑它不會工作,因爲我沒有在服務器端有window.hash.location(它不反正工作)
var client = new FacebookClient(FacebookContext.Current);
dynamic me = client.Get("me");
string firstName = me.first_name;
string lastName = me.last_name;
string email = me.email;
雖然我不能肯定,獲得訪問令牌將解決我的問題,不是嗎? 這是我得到的錯誤。
(OAuthException)一種活動接入 令牌必須用於查詢有關當前用戶 信息。
我在做什麼錯?
+1正在實現相同的代碼。實際上我的方案與redirect_uri。那個例子只在winforms上有。我試圖將其轉換爲webforms – naveen 2011-03-09 06:22:42