使用 RestSharp 为 Etsy 的 API 获取签名无效调用 oauth/request_token [英] Getting signature_invalid calling oauth/request_token for Etsy's API using RestSharp

查看:33
本文介绍了使用 RestSharp 为 Etsy 的 API 获取签名无效调用 oauth/request_token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RestSharp 来访问 Etsy 的 API.这是我尝试获取 OAuth 访问令牌时使用的代码:

I'm trying to use RestSharp to access Etsy's API. Here's the code I'm using attempting to get an OAuth access token:

        var authenticator = OAuth1Authenticator.ForRequestToken(
            ConfigurationManager.AppSettings["ApiKey"],
            ConfigurationManager.AppSettings["ApiSecret"]);

        // same result with or without this next line:
        // authenticator.ParameterHandling = OAuthParameterHandling.UrlOrPostParameters;

        this.Client.Authenticator = authenticator;

        var request = new RestRequest("oauth/request_token")
            .AddParameter("scope", "listings_r");

        var response = this.Client.Execute(request);

Etsy 告诉我签名无效.有趣的是,当我将请求生成的时间戳和随机数值输入到这个 OAuth 签名验证工具,签名不匹配.此外,该工具生成的 URL 可与 Etsy 一起使用,而 RestSharp 生成的 URL 则无法使用.是我做错了什么还是我需要用 RestSharp 配置其他东西?

Etsy tells me that the signature is invalid. Interestingly enough, when I enter the timestamp and nonce values generated by the request into this OAuth signature validation tool, the signatures don't match. Moreover, the URL generated by the tool works with Etsy where the one generated by RestSharp doesn't. Is there something I'm doing wrong or something else I need to configure with RestSharp?

注意:我使用的是他们的 Nuget 包提供的 RestSharp 版本,在发布时为 102.5.

Note: I'm using the version of RestSharp provided by their Nuget package, which at the time of this posting is 102.5.

推荐答案

我终于能够使用 OAuth 通过 RestSharp 连接到 Etsy API.这是我的代码——我希望它对你有用...

I finally was able to connect to the Etsy API with RestSharp using OAuth. Here is my code -- I hope it works for you...

RestClient mRestClient = new RestClient();

//mRestClient.BaseUrl = API_PRODUCTION_URL;
mRestClient.BaseUrl = API_SANDBOX_URL;
mRestClient.Authenticator = OAuth1Authenticator.ForRequestToken(API_KEY, 
                                              API_SHAREDSECRET, 
                                              "oob");

RestRequest request = new RestRequest("oauth/request_token", Method.POST);
request.AddParameter("scope", 
                     "shops_rw transactions_r transactions_w listings_r listings_w listings_d");

RestResponse response = mRestClient.Execute(request);

if (response.StatusCode != System.Net.HttpStatusCode.OK)
   return false;

NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(response.Content);

string oauth_token_secret = queryString["oauth_token_secret"];
string oauth_token = queryString["oauth_token"];

string url = queryString["login_url"];
System.Diagnostics.Process.Start(url);

// BREAKPOINT HERE
string oauth_token_verifier = String.Empty; // get from URL

request = new RestRequest("oauth/access_token");
mRestClient.Authenticator = OAuth1Authenticator.ForAccessToken(API_KEY,
                           API_SHAREDSECRET,
                           oauth_token,
                           oauth_token_secret,
                           oauth_token_verifier);
response = mRestClient.Execute(request);

if (response.StatusCode != System.Net.HttpStatusCode.OK)
  return false;

queryString = System.Web.HttpUtility.ParseQueryString(response.Content);

string user_oauth_token = queryString["oauth_token"];
string user_oauth_token_secret = queryString["oauth_token_secret"];

user_oauth_token 和 user_oauth_token_secret 是用户的访问令牌和访问令牌秘密——这些对用户有效,直到用户撤销访问.

The user_oauth_token and user_oauth_token_secret are the user's access token and access token secret -- these are valid for the user until the user revokes access.

希望这段代码有帮助!

这篇关于使用 RestSharp 为 Etsy 的 API 获取签名无效调用 oauth/request_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆