nin*_*ino 6 c# restsharp oauth-2.0 bexio
我正在尝试构建一个 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;
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何获得授权令牌并提供我需要的所有信息。有人可以告诉我我缺少什么吗?
https://docs.bexio.com/#section/Authentication/Authorization-Code-Flow
您正在尝试使用客户端凭据流,这对于无需用户身份验证的控制台应用程序来说是有意义的。
不过,Bexio 似乎只支持授权码流程。这取决于浏览器重定向来验证用户身份并返回授权代码。然后在后续的令牌调用中,您传入client_id=<your client id>,grant_type=authorization_code和code=<your code>。
解决此问题的唯一方法是构建一个 Web 应用程序,充当授权部分的 OAuth2 客户端,显示返回的授权代码,而不是将其交换为令牌。让用户将该代码作为输入参数复制/粘贴到控制台应用程序,然后控制台应用程序在令牌调用中使用客户端凭据。
或者您需要将授权代码流的授权部分构建到 HTTP 级别的控制台应用程序中。但这非常脆弱,因为授权服务器端的任何更改都可能会破坏控制台应用程序。
| 归档时间: |
|
| 查看次数: |
8387 次 |
| 最近记录: |