gpt4 book ai didi

c# - Facebook C# SDK 获取用户accessToken

转载 作者:太空宇宙 更新时间:2023-11-03 19:17:59 25 4
gpt4 key购买 nike

我在从登录 Facebook 的结果中获取 accessToken 时遇到问题。在 Page_load 中,我的 url 没有被正确解析。

GenerateLoginUrl 方法工作正常,我可以在 QueryString 中看到 accessToken。我试过从中获取它,但它也不起作用。

    protected void Page_Load(object sender, EventArgs e)
{
var fb = new FacebookClient();

FacebookOAuthResult result;
if (fb.TryParseOAuthCallbackUrl(Request.Url, out result))
{
if (result.IsSuccess)
{
// result clicked authorized
var accessToken = result.AccessToken;
Label1.Text = accessToken.ToString();
}
else
{
// user clicked cancelled and didn't authorize the app
var error = result.ErrorDescription;
Label1.Text = error;
}
}
else
{
//This is were it always goes
Label1.Text = "not valid facebook url";
}


}


private Uri GenerateLoginUrl(string appId, string extendedPermissions)
{

try
{
dynamic parameters = new ExpandoObject();
parameters.client_id = appId;
parameters.redirect_uri = "local iis ip";

// The requested response: an access token (token), an authorization code (code), or both (code token).
parameters.response_type = "token";

// list of additional display modes can be found at http://developers.facebook.com/docs/reference/dialogs/#display
parameters.display = "popup";

// add the 'scope' parameter only if we have extendedPermissions.
if (!string.IsNullOrWhiteSpace(extendedPermissions))
parameters.scope = extendedPermissions;

// generate the login url
var fb = new FacebookClient();

var url = fb.GetLoginUrl(parameters);

return url;
}

catch (FacebookOAuthException fbex)
{
Label1.Text = fbex.Message;
return null;

}

catch (Exception ex)
{
Label1.Text = ex.Message;
return null;
}


}


protected void Button1_Click(object sender, EventArgs e)
{
string AppId = ConfigurationManager.AppSettings["apiKey"];
string ExtendedPermissions = ConfigurationManager.AppSettings["ExtendedPermissions"];

var url = GenerateLoginUrl(AppId, ExtendedPermissions);

Response.Redirect(url.AbsoluteUri, false);
}

感谢您的帮助。

更新:

在 Request.Url 中,url 始终是:

http://localhost:23560/Appname/default.aspx

但我可以在浏览器中看到真实的 url,它是:

http://localhost:23560/Appname/#access_token=0000000000&expires_in=3821

那么 asp.net 就不能正确读取 url 吗?

最佳答案

我找到了答案软件,它是:

您必须获取代码而不是 token ,代码将显示在查询字符串中 accesstoken 被删除。

 parameters.response_type = "token";

交流

 parameters.response_type = "code";

然后兑换code to token

 dynamic token = fb.Get("oauth/access_token", new
{
client_id = "appid",
client_secret = "appsecret",
redirect_uri = url,
code = Request.QueryString["code"]
});

var accessToken = token.access_token;

关于c# - Facebook C# SDK 获取用户accessToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14754312/

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