使用c#RestSharp访问Etsy API oauth

时间:2013-09-18 15:05:11

标签: c# oauth restsharp etsy

我在我们的商店下设置了一个开发者帐户,以访问我们的销售收据。我决定使用RestSharp来提出我的请求。我已经证明它适用于没有Oauth要求的电话。我已经成功收到了accessToken和accessTokenSecret。所以我使用它们与customerKey和customerSecret进行ForProtectedResource调用,对于oauth请求如下,但总是收到“此方法需要身份验证”。

我希望它能让我感到很简单。我想,我需要打电话的是这四个项目是否正确?一旦我有了这四个项目,我就不再需要或访问令牌了,对吗?感谢

        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 个答案:

答案 0 :(得分:4)

经过一些反复试验后,我终于围绕着OAuth以及Etsy实现它的方式。 api_key参数仅在您调用无OAuth所需方法时使用。否则你必须发送所有必需的OAuth参数。以下是工作代码。我利用了RestSharp,以及我找到的这个OAuth基础here。希望这能帮助一些可怜的傻瓜盯着糟糕的代码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;
        });