gpt4 book ai didi

c# - 使用 Facebook C# SDK 在登陆页面上获取 AccessToken

转载 作者:行者123 更新时间:2023-11-30 18:05:48 24 4
gpt4 key购买 nike

编码平台 ASP.NET 4.0 WebForms

我有两个相关的页面

  1. 登录.aspx
  2. 着陆页.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 "";
}
}

那部分工作正常。但是我对如何在我的 redirect_uri 的 LandingPage 上访问用户信息一无所知。已经试过了。

    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;

虽然我不确定,但获取访问 token 会解决我的问题,不是吗?这是我遇到的错误。

(OAuthException) An active access token must be used to query information about the current user.

我做错了什么?

最佳答案

这是一个使用网络表单的独立网站示例。查看Default.aspx.csWeb.config .请注意,可以修改此示例以用于 latest source code并且可能不适用于最新版本(5.0.3 测试版)。

关于c# - 使用 Facebook C# SDK 在登陆页面上获取 AccessToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5234592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com