【问题标题】:OAuth2 Access token in console application控制台应用程序中的 OAuth2 访问令牌
【发布时间】:2020-03-12 21:39:57
【问题描述】:

我正在尝试构建一个 C# dotnetcore 控制台应用程序来与使用 OAuth2 进行授权的 api 交互。我还没有完全弄清楚如何将它用于 C# 控制台应用程序,甚至是不打算让用户登录的库。这可能吗?

到目前为止,我得到以下内容:

    private static string _auth_url = "https://idp.bexio.com/authorize";
    private static string _token_url = "https://idp.bexio.com/token";
    private static string _callback_url = "https://www.myurl.com";

    private static string _scope = "monitoring_show";
    private static string _client_id = "myid";
    private static string _client_secret = "mysecret";
    static void Main(string[] args)
    {
        //request token
        var restclient = new RestClient(_auth_url);
        RestRequest request = new RestRequest("request/oauth") {Method = Method.POST};
        request.AddHeader("Accept", "application/json");
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        request.AddParameter("client_id", _client_id);
        request.AddParameter("client_secret", _client_secret);
        request.AddParameter("grant_type", "client_credentials");
        var tResponse = restclient.Execute(request);
        var responseJson = tResponse.Content;
    }  

我不知道如何获得授权令牌并提供我需要的所有信息。有人可以告诉我我缺少什么吗?

https://docs.bexio.com/#section/Authentication/Authorization-Code-Flow

【问题讨论】:

    标签: c# oauth-2.0 restsharp bexio


    【解决方案1】:

    您正在尝试使用客户端凭据流,这对于没有用户身份验证的控制台应用程序是有意义的。

    但是,Bexio 似乎只支持授权码流。这取决于浏览器重定向来验证用户并返回授权码。然后在subsequent token call 中,传入client_id=<your client id>grant_type=authorization_codecode=<your code>

    解决此问题的唯一方法是构建一个 Web 应用程序,该应用程序充当授权部分的 OAuth2 客户端,显示返回的授权代码,而不是将其交换为令牌。让用户将该代码作为输入参数复制/粘贴到控制台应用程序,然后在令牌调用中使用客户端凭据。

    或者您需要在 HTTP 级别将授权代码流的authorize part 构建到控制台应用程序中。但这非常脆弱,因为授权服务器端的任何更改都可能会破坏控制台应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-17
      • 2013-04-11
      • 1970-01-01
      • 2020-11-23
      • 2011-03-07
      • 2015-04-05
      • 2015-01-02
      相关资源
      最近更新 更多