5

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 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?

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

Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165

1 Answers1

7

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"];

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.

I hope this code helps!

Stephen
  • 1,737
  • 2
  • 26
  • 37
kris
  • 71
  • 3
  • @Daniel Schaffer, did Kris's solution work for you? I ask because when I try it with my consumer key and secret (but posting to https://openapi.etsy.com/v2/), I get Unauthorised / Signature invalid. This is driving me mad. – err1 Jul 07 '17 at 14:53
  • Basically this code worked for me to _get_ the permanent tokens, but I'm having the 'signature invalid' problem when _using_ those values with `OAuth1Authenticator.ForProtectedResource(appKey, appSecret, accessKey, accessSecret);` – drzaus Oct 09 '18 at 14:34