如何使用C#中的OAuth-2.0从Etsy请求刷新令牌

r3i60tvu  于 2023-01-16  发布在  C#
关注(0)|答案(2)|浏览(135)

在使用C#代码获取Etsy OAuth 2令牌失败后,我开始使用Postman进行初始请求,认为在需要时请求刷新令牌将是一件简单的事情。因此,从Postman,一切都很好,我可以登录并授予访问权限。然后Etsy会用一个短期访问令牌来响应(以及用于请求另一个接入令牌而不必再次经历整个授权过程的刷新令牌)。
然而,我不知道如何使用这个刷新令牌获得另一个访问令牌,因为Etsy一直在响应grant_type是必需的。
好的,成功授予访问权限后, Postman 的初始响应是:

{
    "access_token": "<initial-access-token-from_Etsy>",
    "token_type": "Bearer",
    "expires_in": 3600,

    "refresh_token": "<my-refresh-token>"
}

按照他们的文档请求刷新OAuth令牌,这意味着我所需要做的就是向https://openapi.etsy.com/v3/public/oauth/token发出POST请求,并将以下参数添加到内容类型为的JSON主体:应用程序/x-www-格式-网址编码:

{
   "grant_type": "refresh_token",
   "client_id": "<my-client-API-string>",
   "refresh_token": "<my-refresh-token>"
}

因此,在Postman中设置所有这些并查看为请求生成的代码,可以得到:

var client = new RestClient("https://openapi.etsy.com/v3/public/oauth/token");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("x-api-key", "<my-client-API-string>");*
request.AddParameter("grant_type", "refresh_token");
request.AddParameter("client_id", "<my-client-API-string>");
request.AddParameter("refresh_token", "<my-refresh-token>");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
  • 注意-我必须在标头中添加参数x-api-key,否则它将失败并显示“error”:“缺少x-api-key标头或client_id”

唉,Etsy的回应总是

{
    "error": "invalid_request",
    "error_description": "grant_type is required"
}

这个post guided me in making the initial request
当grant_type按照请求出现在主体中时,它为什么抱怨它!?!?!?

v7pvogib

v7pvogib1#

解决了!:我必须首先弄清楚@GH DevOps是关于什么的(没有尝试@Abdelkrim Bournane的),但最终想出了以下解决方案:

public async void RenewRefreshToken()
{
    string contents;
    string url = "https://openapi.etsy.com/v3/public/oauth/token";
    Dictionary<string, string> data = new Dictionary<string, string>
    {
        { "grant_type", "refresh_token" },
        { "client_id", "<my-client-API-string>>" },
        { "refresh_token", "<my-refresh-token>" }
    };

    var httpClient = new HttpClient();
    var response = await httpClient.PostAsync(url, new FormUrlEncodedContent(data));

    //
    // Response from Etsy will be as documented on their website: https://developer.etsy.com/documentation/essentials/authentication/#requesting-a-refresh-oauth-token
    //
    // access token will be valid for another 1hr
    // Thereater, renew again using the returned refresh token
    //
    contents = await response.Content.ReadAsStringAsync();
}

响应是JSON,包含新的访问令牌和刷新令牌**。

{
    "access_token":"<my-new-access-token>",
    "token_type":"Bearer",
    "expires_in":3600,
    
    "refresh_token":"<my-new-refresh-token>"
}
  • 编辑日期:2022年2月9日 *
    **请注意,当您续订访问令牌时,响应是一个新的访问令牌(自发出之日起1小时内有效)和一个新的刷新令牌(自发出之日起90天内有效),至少我感到困惑。因此,只要这些刷新令牌未过期,您就可以继续收集和使用它们!
vbopmzt1

vbopmzt12#

Error401 error = mapper.readValue(response.getBody(), Error401.class);
Unirest.setTimeouts(0, 0);
response = Unirest.post(endpoint.concat("public/oauth/token"))
    .field("grant_type", "refresh_token")
    .field("client_id", properties.getProperty("x-api-key"))
    .field("refresh_token", properties.getProperty("access.refresh.token"))
    .asString();
RefreshToken refreshToken = mapper.readValue(response.getBody(), 
    RefreshToken.class);
properties.setProperty("access.token", refreshToken.getAccess_token());
properties.setProperty("access.refresh.token", refreshToken.getRefresh_token());
properties.storeToXML(new FileOutputStream(new File("src/main/resources/config/oauth.xml")),"Etsy Desktop Management Tool v1",
Charset.forName("UTF-8"));
JOptionPane.showMessageDialog(rootPane, error.getError_description() + "\nAccess Token Refreshed !");

相关问题