使用 c# RestSharp 访问 Etsy API oauth [英] Access Etsy API oauth using c# RestSharp
问题描述
我在我们的商店下建立了一个开发者帐户,以访问我们的销售收据.我决定使用 RestSharp 来提出我的要求.我已经证明它适用于不需要 Oauth 的调用.我已成功收到我的 accessToken 和 accessTokenSecret.因此,我将它们与 customerKey 和 customerSecret 一起用于进行 ForProtectedResource 调用,对于 oauth 请求,如下所示,但始终收到此方法需要身份验证".
I set up a developer acct under our shop, to access our sales receipts. I decided to use RestSharp to make my requests. I have proved it works for none Oauth required calls. I have successfully received my accessToken and 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".
我希望它是我遗漏的一些简单的东西.我想,我需要拨打的所有电话都是这四项正确的吗?一旦我拥有这四个项目,我就不再需要请求或访问令牌,对吗?谢谢
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;
});
推荐答案
经过反复试验,我终于把我的心思放在了 OAuth 和 Etsy 实现它的方式上.api_key 参数仅在您调用不需要 OAuth 的方法时使用.否则,您必须将所有必需的 OAuth 参数发送给它.下面是工作代码.我利用了 RestSharp,以及我在此处找到的这个 OAuth 基础.希望这能帮助一些可怜的人不要盯着蹩脚的代码 3 天(就像你一样).
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;
});
这篇关于使用 c# RestSharp 访问 Etsy API oauth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!