Access Etsy API oauth using c# RestSharp

◇◆丶佛笑我妖孽 提交于 2019-12-08 21:25:31

After some trial and error I finally wrapped my head around OAuth and the way Etsy implements it. The api_key parameter is only to be used when you're calling a none OAuth required method. Otherwise you have to send it all the required OAuth params. Below is working code. I leveraged RestSharp, as well as this OAuth base I found here. Hope this help some poor sap from staring at crappy code for 3 days (like yours truly).

        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;
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!