我正在尝试在他们的文档中提供样本授权请求(或任何需要身份验证的Etsy api).我得到的响应是"oauth_problem = token_rejected".
我用这个苏答案与沿OAuth的基础是benSharper挂钩.
我看着这个和这个,和其他人.其中一个使用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
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
Mic*_*ida 11
弄清楚我错过了什么.我错过了获取令牌凭证,这是您访问受保护资源所需的永久令牌.
难以立刻绕过OAuth,RestSharp和Etsy的实施.不需要OAuthBase,RestSharp负责这一点.
请注意,当使用RestSharp进行OAuth调用时,appKey并且sharedSecret成为consumerKey和consumerSecret.
这是工作代码:
/// <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;
}
}
Run Code Online (Sandbox Code Playgroud)
伪代码(带回调):
Etsy_portal对象呼叫GetConfirmUrl,提供回叫URL.回调将有两个查询参数oauth_token和oauth_verifier.以下是回调函数签名的示例:
[HttpGet] public ActionResult EtsyCallback(string oauth_token,string oauth_verifier)
将返回的标记和秘密保存在地图结构中以便以后检索.
GetConfirmUrl.ObtainTokenCredentials获取永久令牌和秘密.