简体   繁体   English

使用c#RestSharp访问Etsy API oauth

[英]Access Etsy API oauth using c# RestSharp

I set up a developer acct under our shop, to access our sales receipts. 我在我们的商店下面设置了一个开发人员帐户,以访问我们的销售收据。 I decided to use RestSharp to make my requests. 我决定使用RestSharp来提出我的请求。 I have proved it works for none Oauth required calls. 我已经证明它适用于没有Oauth要求的电话。 I have successfully received my accessToken and accessTokenSecret. 我已经成功收到了accessToken和accessTokenSecret。 So i use those along with the customerKey and customerSecret to make a ForProtectedResource call, for a oauth request as follows but always receive "This method requires authentication". 所以我使用它们与customerKey和customerSecret进行ForProtectedResource调用,对于oauth请求如下,但总是收到“此方法需要身份验证”。

I'm hoping its something simple I'm missing. 我希望它能让我感到很简单。 I thought, all I need to make any call are those four items correct? 我想,我需要打电话的是这四个项目是否正确? Once I have those four items I don't have to request or access token anymore, correct? 一旦我有了这四个项目,我就不再需要或访问令牌了,对吗? 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;
        });

After some trial and error I finally wrapped my head around OAuth and the way Etsy implements it. 经过一些反复试验后,我终于围绕着OAuth和Etsy实现它的方式。 The api_key parameter is only to be used when you're calling a none OAuth required method. api_key参数仅在您调用无OAuth所需方法时使用。 Otherwise you have to send it all the required OAuth params. 否则你必须发送所有必需的OAuth参数。 Below is working code. 以下是工作代码。 I leveraged RestSharp, as well as this OAuth base I found here . 我利用了RestSharp,以及我在这里找到的OAuth基础。 Hope this help some poor sap from staring at crappy code for 3 days (like yours truly). 希望这有助于一些可怜的傻瓜盯着糟糕的代码3天(就像你的真实)。

        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;
        });

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

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