在 c# 中使用 oauth 从 api 接收访问令牌
Receving access token from api with oauth in c#
我想使用 Newsletter2Go API。我已经下载了一个 c# 示例,添加了已编译的库并尝试开始接收访问令牌。我是使用 REST-API 进行编程的新手,但据我所知,我需要连接我的 api-密钥和凭据才能接收访问令牌。有了token我就可以做进一步的操作了。
这是接收令牌的代码。
//////// Configure HTTP basic authorization: Basic
Configuration.Default.Username = "";
Configuration.Default.Password = "";
var apiInstance = new AuthorizationApi();
var grantType = ""; // string | grant_type (default to https://nl2go.com/jwt)
var username = ""; // string | username. Required for grant_type https://nl2go.com/jwt (optional)
var password = ""; // string | password. Required for grant_type https://nl2go.com/jwt (optional)
var refreshToken = ""; // string | refresh_token. Required for grant_type https://nl2go.com/jwt_refresh (optional)
try
{
// Endpoint for retrieving a token
Token result = apiInstance.GetToken(grantType, username, password, refreshToken);
Debug.WriteLine(result);
}
catch (Exception ex)
{
Debug.Print("Exception when calling AuthorizationApi.GetToken: " + ex.Message);
}
我收到错误。
Exception when calling AuthorizationApi.GetToken:
Error calling GetToken: {"error":"invalid_client","error_description":"The client credentials are invalid"}
我不明白如何将 api 键传递给 api。他们在文档中写了这个,但是文档中的 c# 中没有示例。
...and add an Authorization header with your auth key:
xhr.setRequestHeader("Authorization", "Basic " + btoa("xhr5n6xf_Rtguwv_jzr1d3_LTshikn4_0dtesdahNvp1:Kqf2Hs#Wwazl");
send the request:
xhr.send(JSON.stringify(params));
所以这里缺少什么想法?提前致谢。
您需要在请求header中传递api_key。您可以使用 RestSharp:
执行以下操作
var client = new RestClient("https://api.newsletter2go.com/oauth/v2/token");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic xhr5n6xf_Rtguwv_jzr1d3_LTshikn4_0dtesdahNvp1:Kqf2Hs#Wwazl");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=https://nl2go.com/jwt&username={username}&password={password}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
然后从响应 body 中获取您的访问令牌。
我想使用 Newsletter2Go API。我已经下载了一个 c# 示例,添加了已编译的库并尝试开始接收访问令牌。我是使用 REST-API 进行编程的新手,但据我所知,我需要连接我的 api-密钥和凭据才能接收访问令牌。有了token我就可以做进一步的操作了。
这是接收令牌的代码。
//////// Configure HTTP basic authorization: Basic
Configuration.Default.Username = "";
Configuration.Default.Password = "";
var apiInstance = new AuthorizationApi();
var grantType = ""; // string | grant_type (default to https://nl2go.com/jwt)
var username = ""; // string | username. Required for grant_type https://nl2go.com/jwt (optional)
var password = ""; // string | password. Required for grant_type https://nl2go.com/jwt (optional)
var refreshToken = ""; // string | refresh_token. Required for grant_type https://nl2go.com/jwt_refresh (optional)
try
{
// Endpoint for retrieving a token
Token result = apiInstance.GetToken(grantType, username, password, refreshToken);
Debug.WriteLine(result);
}
catch (Exception ex)
{
Debug.Print("Exception when calling AuthorizationApi.GetToken: " + ex.Message);
}
我收到错误。
Exception when calling AuthorizationApi.GetToken:
Error calling GetToken: {"error":"invalid_client","error_description":"The client credentials are invalid"}
我不明白如何将 api 键传递给 api。他们在文档中写了这个,但是文档中的 c# 中没有示例。
...and add an Authorization header with your auth key:
xhr.setRequestHeader("Authorization", "Basic " + btoa("xhr5n6xf_Rtguwv_jzr1d3_LTshikn4_0dtesdahNvp1:Kqf2Hs#Wwazl");
send the request:
xhr.send(JSON.stringify(params));
所以这里缺少什么想法?提前致谢。
您需要在请求header中传递api_key。您可以使用 RestSharp:
执行以下操作var client = new RestClient("https://api.newsletter2go.com/oauth/v2/token");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic xhr5n6xf_Rtguwv_jzr1d3_LTshikn4_0dtesdahNvp1:Kqf2Hs#Wwazl");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=https://nl2go.com/jwt&username={username}&password={password}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
然后从响应 body 中获取您的访问令牌。