Access Etsy oauth API using C # RestSharp

I set up a developer share in our store in order to access our sales receipts. I decided to use RestSharp to fulfill my requests. I have proven that this works, no one is needed for Out. I successfully got my access to Token and accessTokenSecret. Therefore, I use them together with customerKey and customerSecret to call ForProtectedResource to request oauth as follows, but always get "This method requires authentication."

I hope that something simple is missing me. I thought all I need to call is these four points correct? Once I have these four elements, I no longer need to request or access the token, right? Thanks

var access_token = "#########################"; var access_token_secret = "########"; var baseUrl = "https://openapi.etsy.com/v2"; var client = new RestClient(baseUrl); client.Authenticator = OAuth1Authenticator.ForProtectedResource(consumerKey, consumerSecret, access_token, access_token_secret); var request = new RestRequest("shops/########/receipts"); request.Method = Method.GET; request.AddParameter("api_key", consumerKey); client.ExecuteAsync(request, response => { var r = response; }); 
+1
source share
1 answer

After some trial and error, I finally wrapped my head around OAuth and how Etsy implements it. The api_key parameter is used only when calling the not required OAuth method. Otherwise, you need to send all the necessary OAuth parameters. Below is the working code. I used RestSharp, as well as this OAuth base, which I found here . Hope this helps the poor juice look at the crappy code for 3 days (as it actually is).

  var restClient = new RestClient(baseUrl); OAuthBase oAuth = new OAuthBase(); string nonce = oAuth.GenerateNonce(); string timeStamp = oAuth.GenerateTimeStamp(); string normalizedUrl; string normalizedRequestParameters; string sig = oAuth.GenerateSignature(new Uri(baseUrl + MethodLocation), consumerKey, consumerSecret, Accesstoken, AccessTokenSecret, "GET", timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters); // sig = HttpUtility.UrlEncode(sig); var request = new RestRequest(MethodLocation); request.Resource = string.Format(MethodLocation); request.Method = Method.GET; // request.AddParameter("api_key", consumerKey); request.AddParameter("oauth_consumer_key", consumerKey); request.AddParameter("oauth_token", Accesstoken); request.AddParameter("oauth_nonce", nonce); request.AddParameter("oauth_timestamp", timeStamp); request.AddParameter("oauth_signature_method", "HMAC-SHA1"); request.AddParameter("oauth_version", "1.0"); request.AddParameter("oauth_signature", sig); restClient.ExecuteAsync(request, response => { var r = response; }); 
+4
source

Source: https://habr.com/ru/post/1206971/


All Articles