简体   繁体   English

使用RestSharp为Etsy的API获取signature_invalid调用oauth / request_token

[英]Getting signature_invalid calling oauth/request_token for Etsy's API using RestSharp

I'm trying to use RestSharp to access Etsy's API. 我正在尝试使用RestSharp来访问Etsy的API。 Here's the code I'm using attempting to get an OAuth access token: 这是我尝试获取OAuth访问令牌的代码:

        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 tells me that the signature is invalid. Etsy告诉我签名无效。 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. 有趣的是,当我将请求生成的时间戳和随机数值输入到此OAuth签名验证工具中时 ,签名不匹配。 Moreover, the URL generated by the tool works with Etsy where the one generated by RestSharp doesn't. 此外,该工具生成的URL与Etsy一起使用,其中RestSharp生成的URL不支持。 Is there something I'm doing wrong or something else I need to configure with RestSharp? 有什么我做错了或我需要用RestSharp配置的其他东西?

Note: I'm using the version of RestSharp provided by their Nuget package, which at the time of this posting is 102.5. 注意:我正在使用他们的Nuget包提供的RestSharp版本,该版本在发布时为102.5。

I finally was able to connect to the Etsy API with RestSharp using OAuth. 我终于能够使用OAuth与RestSharp连接到Etsy API。 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"];

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. user_oauth_token和user_oauth_token_secret是用户的访问令牌和访问令牌秘密 - 这些对用户有效,直到用户撤销访问权限。

I hope this code helps! 我希望这段代码有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM