Etsy oauth认证c#RestSharp

时间:2014-11-16 07:46:10

标签: c# api oauth etsy

我正在尝试在documentation中提供样本授权请求(或任何需要身份验证的Etsy api)。我得到的回答是“oauth_problem = token_rejected”。

我使用this SO answer以及OAuth base链接到的benSharper

我查看了thisthis以及其他人。其中一个使用https://sandbox.https://openapi.etsy.com/v2,当我尝试这个时,异常是“底层连接已关闭:无法建立SSL / TLS安全通道的信任关系。”我部署到我的服务器(这是https)并仍然是相同的响应。

似乎无法让它发挥作用。我错过了什么?

这是我的代码:

public class AuthorizedRequestHelper
    {
        string baseUrl = "https://openapi.etsy.com/v2";
        string relativePath = "/oauth/scopes";
        string oauth_consumer_key = "xxx";
        string consumerSecret = "xxx";
        string oauth_token = "xxx";
        string oauth_token_secret = "xxx";

        public void test()
        {
            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 + relativePath), oauth_consumer_key, consumerSecret, oauth_token, oauth_token_secret, "GET", timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);


            var request = new RestRequest(relativePath);
            request.Resource = string.Format(relativePath);
            request.Method = Method.GET;

            request.AddParameter("oauth_consumer_key", oauth_consumer_key);
            request.AddParameter("oauth_token", oauth_token);
            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);

            IRestResponse irestResponse = restClient.Execute(request);
            var content = irestResponse.Content;
            // content = oauth_problem=token_rejected
        }
    }  

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:11)

弄清楚我错过了什么。我错过了Obtaining Token Credentials,这是您访问受保护资源所需的永久令牌。

在OAuth,RestSharp和Etsy的实施过程中遇到麻烦。不需要OAuthBase,RestSharp会解决这个问题。

请注意,使用RestSharp进行OAuth调用时,appKeysharedSecret会变为consumerKeyconsumerSecret

此处的工作代码:

    /// <summary>
    /// RestSharp documentation: https://github.com/restsharp/RestSharp/wiki
    /// </summary>
    public class Etsy_portal
    {
        Uri BASE_URL = new Uri("https://openapi.etsy.com/v2/");

        string appKey;
        string sharedSecret;
        RestClient restClient;

        private string[] _permissions_array;
        public string Permissions
        {
            get { return string.Join(" ", _permissions_array); }
        }

        public Etsy_portal(string appKey_, string sharedSecret_)
        {
            appKey = appKey_;
            sharedSecret = sharedSecret_;

            restClient = new RestClient(BASE_URL);

            //todo move permissions to Web.config
            _permissions_array = new string[] { "listings_r", "listings_w", "listings_d", "shops_rw" };
        }

        public string GetConfirmUrl(out string oauth_token, out string oauth_token_secret, string callbackUrl_ = null)
        {
            restClient.Authenticator = OAuth1Authenticator.ForRequestToken(appKey, sharedSecret, callbackUrl_ ?? "oob");

            RestRequest restRequest = new RestRequest("oauth/request_token", Method.POST);

            restRequest.AddParameter("scope", Permissions);

            IRestResponse response = restClient.Execute(restRequest);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                oauth_token = null;
                oauth_token_secret = null;
                return null;
            }

            NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(response.Content);

            oauth_token = queryString["oauth_token"];
            oauth_token_secret = queryString["oauth_token_secret"];

            return queryString["login_url"];
        }

        public void ObtainTokenCredentials(string oauth_token_temp_, string oauth_token_secret_temp_, string oauth_verifier_, out string permanent_oauth_token_, out string permanent_oauth_token_secret_)
        {
            //consumerKey is the appKey you got when you registered your app, same for sharedSecret
            restClient.Authenticator = OAuth1Authenticator.ForAccessToken(appKey, sharedSecret, oauth_token_temp_, oauth_token_secret_temp_, oauth_verifier_);

            RestRequest restRequest = new RestRequest("oauth/access_token", Method.GET);
            IRestResponse irestResponse = restClient.Execute(restRequest);

            NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(irestResponse.Content);

            permanent_oauth_token_ = queryString["oauth_token"];
            permanent_oauth_token_secret_ = queryString["oauth_token_secret"];
        }

        public string GetScopes(string accessToken_, string accessTokenSecret_)
        {
            restClient.Authenticator = OAuth1Authenticator.ForProtectedResource(appKey, sharedSecret, accessToken_, accessTokenSecret_);

            RestRequest restRequest = new RestRequest("oauth/scopes", Method.GET);

            IRestResponse irestResponse = restClient.Execute(restRequest);

            return irestResponse.Content;
        }
    }

伪代码(带回调):

  1. 构建Etsy_portal对象
  2. 致电GetConfirmUrl,提供回调网址。回调将有两个查询参数oauth_tokenoauth_verifier。这是回调函数签名的一个示例:

    [HttpGet] public ActionResult EtsyCallback(string oauth_token,string oauth_verifier)

  3. 将返回的令牌和机密保存在地图结构中,以便日后检索。

  4. 访问通过GetConfirmUrl
  5. 调用返回的确认网址
  6. 在回调函数中,使用提供的标记(上例中的第一个参数)查找步骤3中保存的密码。
  7. 使用验证者(上例中回调函数的第二个参数),令牌和秘密,调用ObtainTokenCredentials获取永久令牌和秘密。
  8. 保存永久令牌和密码,您可以丢弃步骤1-4中获得的验证者,临时令牌和临时秘密。