未能检索.NET桌面应用程序的访问令牌令牌、应用程序、桌面、NET

2023-09-05 23:32:12 作者:绝望的歌

我正在写一个.NET应用程序,在Windows计算机上运行。它不是通过浏览器访问。问题是,我不能验证像我应该的。我目前编码C#.NET,更具体的在C#。

在我有我的形式WebBrowser控件。 在用户登录到Facebook的通过这个WebBrowser控件。 在登录之后,我开始认证过程。 然后我retreive一个code。

下面就是它出了问题。有了这个code我想获得一个访问令牌。 所生成的请求URL的样子: https://graph.facebook.com/oauth/access_token?client_id=____MY_APP_ID______&redirect_uri=http://localhost/&client_secret=_____MY_APP_SECRET_____&$c$c=____MY_RETREIVED_$c$c_____并通过以下code制成。

请注意,我重定向URL是的http://本地主机。这应该没问题吧?

此外,在我的应用程序设置,我有如下信息。

  

网站网址:的http://本地主机/   网站域名:本地主机

 私人字符串交易所codeForToken(字符串code,乌里的redirectUrl)
{
    VAR TokenEndpoint =新的URI(https://graph.facebook.com/oauth/access_token);
    VAR URL = TokenEndpoint +? +
                      CLIENT_ID =+ _AppID +与& +
                      redirect_uri =+的redirectUrl +与& +
                      client_secret =+ _AppSecret +与& +
                      code =+ code;
    VAR请求= WebRequest.CreateDefault(新的URI(URL));
    使用(VAR响应= request.GetResponse())
    {
        使用(VAR responseStream = response.GetResponseStream())
        {
            使用(VAR responseReader =新的StreamReader(responseStream))
            {
                VAR的responseText = responseReader.ReadToEnd();
                VAR令牌= responseText.Replace(access_token =,);
                返回令牌;
            }
        }
     }
}
 
使用Cheat Engine与DnSpy破解Unity游戏

当我执行,我得到这个错误:

  

Webexception是未处理由用户code   远程服务器返回错误:(400)错误的请求。

在此处,我想我可能会错误:

是我的应用程序的设置是否正确? 如果我重定向URL是的http://本地主机,即使有也不是一个服务听有?

最重要的是:

我该如何摆脱这种错误的和retreive访问令牌?

在此先感谢!

解决方案

您收到此错误,因为你不应该从一个桌面应用程序调用这个URL:据我所知,你不能使用该令牌端点桌面应用程序的认证。此外,您还可以直接获得访问令牌(不用问了code第一)。这里是你必须做的。

加载以下网址与嵌入式Web浏览器:

  https://www.facebook.com/dia​​log/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.