无法在.NET桌面应用程序中检索访问令牌 [英] Failing to retrieve access token in .NET desktop app
问题描述
- 我的表单上有一个web浏览器控件。
- 用户通过此Web浏览器控件登录到Facebook。
- 登录后,我开始验证过程。
- 然后我再检索一个代码。
这里出错了。有了这个代码,我想获得访问令牌。
生成的请求URL如下所示: https://graph.facebook.com/oauth/access_token?client_id=____MY_APP_ID______&redirect_uri=http://localhost/&client_secret=_____MY_APP_SECRET_____&code = ____ MY_RETREIVED_CODE _____
并通过以下代码进行。
请注意,我的重定向网址是 http:// localhost
。这个应该是可以的,对吗?
此外,在我的应用设置中,我有以下信息。
站点URL:
http:// localhost /
站点域:localhost
private String ExchangeCodeForToken(String code,Uri redirectUrl)
{
var TokenEndpoint = new Uri(https:// graph .facebook.com / OAuth的/的access_token);
var url = TokenEndpoint +? +
client_id =+ _AppID +& +
redirect_uri =+ redirectUrl +& +
client_secret =+ _AppSecret +& +
code =+ code;
var request = WebRequest.CreateDefault(new Uri(url));
使用(var response = request.GetResponse())
{
using(var responseStream = response.GetResponseStream())
{
using(var responseReader = new StreamReader(responseStream))
{
var responseText = responseReader.ReadToEnd();
var token = responseText.Replace(access_token =,);
返回令牌;
}
}
}
}
我执行这个,我得到这个错误:
Webexception未被用户代码
远程服务器返回错误:(400)错误请求。
我认为我可能会出错: p>
- 我的应用设置是否正确?
- 如果我的重定向网址为
http: // localhost
,即使实际上没有在那边听?
最重要的是:
- 如何摆脱这个错误并检索访问令牌?
提前感谢!
你得到这个错误,因为你不应该从桌面应用程序调用这个URL:据我所知,你不能使用令牌端点桌面应用验证。此外,您可以直接获取访问令牌(不需要先询问代码)。
在嵌入式网络浏览器中加载以下URL:
https://www.facebook.com/dialog/oauth?
client_id = YOUR_APP_ID&
redirect_uri = https://www.facebook.com/connect/login_success.html
用户将被要求登录,并将使用URL中的访问令牌重定向到此URL:
https:/ /www.facebook.com/connect/login_success.html#access_token = ...
所以你有检测重定向并从URL中检索访问令牌。
I'm writing a .NET app that runs on a Windows computer. It is not accessible through the browser. The problem is, I can't authenticate like I should. I'm currently coding in C# .NET, more specific in C#.
- I have a webbrowser control on my form.
- The user logs on to facebook through this webbrowser control.
- After the logon, I start the authentication procedure.
- I then retreive a code.
Here's where it goes wrong. With this code I want to obtain an access token.
The generated request URL looks like: https://graph.facebook.com/oauth/access_token?client_id=____MY_APP_ID______&redirect_uri=http://localhost/&client_secret=_____MY_APP_SECRET_____&code=____MY_RETREIVED_CODE_____
and is made through the code below.
Please note that my redirect URL is http://localhost
. This should be okay, right?
Also, in my App Settings, I have the following information.
Site URL:
http://localhost/
Site Domain: localhost
private String ExchangeCodeForToken(String code, Uri redirectUrl)
{
var TokenEndpoint = new Uri("https://graph.facebook.com/oauth/access_token");
var url = TokenEndpoint + "?" +
"client_id=" + _AppID + "&" +
"redirect_uri=" + redirectUrl + "&" +
"client_secret=" + _AppSecret + "&" +
"code=" + code;
var request = WebRequest.CreateDefault(new Uri(url));
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
using (var responseReader = new StreamReader(responseStream))
{
var responseText = responseReader.ReadToEnd();
var token = responseText.Replace("access_token=", "");
return token;
}
}
}
}
When I execute this, I get this error:
Webexception was unhandled by user code The remote server returned an error: (400) Bad Request.
Here's where I think I might be going wrong:
- Are my app settings correct?
- Should my redirect url be
http://localhost
, even if there isn't actually a service listening there?
Most importantly:
- how do I get rid of this error and retreive the access token?
Thanks in advance!
You get this error because you are not supposed to call this URL from a Desktop app : as far as I know, you can not use the token endpoint for Desktop app authentication. Also, you can get the access token directly (no need to ask for a code first). Here is what you have to do.
Load the following URL in your embedded web browser :
https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&
redirect_uri=https://www.facebook.com/connect/login_success.html
The user will be asked to log in and will be redirected to this URL with the access token in the URL :
https://www.facebook.com/connect/login_success.html#access_token=...
So you have to detect the redirect and retrieve the access token from the URL.
这篇关于无法在.NET桌面应用程序中检索访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!