没有人成功使用C#创建应用并使用Etsy API创建新列表。我已经开发了一个应用程序,无论是否具有权限,该应用程序都非常适合检索数据(HTTP GET)。我无法弄清楚的唯一部分是如何为HTTP POST编写有效的代码。
这是一个可以正常工作的HTTP GET示例,用于检索所有不活动的列表:
public EtsyListings GetAllStoreListingsInactive(string storeProviderStoreId, string consumerKey, string consumerSecret, string oAuthToken, string oAuthTokenSecret)
{
string url = "shops/" + storeProviderStoreId + "/listings/inactive";
var request = GenerateSecureRequest(url, RequestType.GET, consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret);
var response = _restClient.Execute<EtsyListings>(request);
if (response.ResponseStatus != ResponseStatus.Completed)
throw new Exception("Retrieving AllStoreListingsInactive Failed");
return response.Data;
}
private RestRequest GenerateSecureRequest(string url, RequestType requestType, string consumerKey, string consumerSecret, string oAuthToken, string oAuthTokenSecret)
{
OAuthBase oAuth = new OAuthBase();
string nonce = oAuth.GenerateNonce();
string timeStamp = oAuth.GenerateTimeStamp();
string normalizedUrl;
string normalizedRequestParameters;
string relativeUri = url;
string sig = oAuth.GenerateSignature(new Uri(BASE_URL.ToString() + relativeUri), consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret, requestType.ToString(), timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);
var request = new RestRequest(relativeUri);
request.Resource = string.Format(relativeUri);
request.Method = Method.GET;
request.AddParameter("oauth_consumer_key", consumerKey);
request.AddParameter("oauth_token", oAuthToken);
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);
return request;
}
这是我到目前为止编写的用于创建新列表的代码,它不起作用:
public EtsyListings CreateListing(string storeProviderStoreId, string consumerKey, string consumerSecret, string oAuthToken, string oAuthTokenSecret)
{
string url = "listings";
var request = GenerateSecureRequestTestingCreatingListing(url, RequestType.POST, consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret);
var response = _restClient.Execute<EtsyListings>(request);
if (response.ResponseStatus != ResponseStatus.Completed)
throw new Exception("Retrieving Creating Listing Failed");
return response.Data;
}
private RestRequest GenerateSecureRequestTestingCreatingListing(string url, RequestType requestType, string consumerKey, string consumerSecret, string oAuthToken, string oAuthTokenSecret)
{
OAuthBase oAuth = new OAuthBase();
string nonce = oAuth.GenerateNonce();
string timeStamp = oAuth.GenerateTimeStamp();
string normalizedUrl;
string normalizedRequestParameters;
string relativeUri = url;
string sig = oAuth.GenerateSignature(new Uri(BASE_URL.ToString() + relativeUri), consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret, requestType.ToString(), timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);
var request = new RestRequest(relativeUri);
request.Resource = string.Format(relativeUri);
request.Method = Method.POST;
request.AddParameter("oauth_consumer_key", consumerKey);
request.AddParameter("oauth_token", oAuthToken);
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);
request.AddParameter("title", "Test Title");
request.AddParameter("description", "Test Description");
request.AddParameter("quantity", "1");
request.AddParameter("price", "1");
request.AddParameter("is_supply", "false");
request.AddParameter("state", "draft");
request.AddParameter("when_made", "2010_2017");
request.AddParameter("who_made", "i_did");
request.AddParameter("shipping_template_id", "43862539760");
var response = _restClient.Execute(request);
return request;
}
有人发现上面的CreatingListing代码有明显的错误吗?
我收到的响应对象的错误代码是“禁止”的“状态代码”和“ oauth_problem = signature_invalid ...”的“内容”
我已验证我具有完全权限,并且签名不是上面提供的HTTP GET示例的问题。
其他信息: 没有添加“ quantity”参数的情况下,我收到一个“ BadRequest”错误,指出“预期参数'quantity'。”
并重新编写代码,所以我将对象传递给正文:
private RestRequest GenerateSecureRequestTestingCreatingListing(string url, RequestType requestType, string consumerKey, string consumerSecret, string oAuthToken, string oAuthTokenSecret)
{
OAuthBase oAuth = new OAuthBase();
string nonce = oAuth.GenerateNonce();
string timeStamp = oAuth.GenerateTimeStamp();
string normalizedUrl;
string normalizedRequestParameters;
string relativeUri = url;
string sig = oAuth.GenerateSignature(new Uri(BASE_URL.ToString() + relativeUri), consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret, requestType.ToString(), timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);
var request = new RestRequest(relativeUri);
request.Resource = string.Format(relativeUri);
request.Method = Method.POST;
request.AddParameter("oauth_consumer_key", consumerKey);
request.AddParameter("oauth_token", oAuthToken);
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);
request.AddParameter("quantity", "1");
request.RequestFormat = DataFormat.Json;
var dataObj = new
{
title = "This is a test1",
description = "Test Description1",
quantity = "1",
price = "1",
is_supply = "false",
state = "draft",
when_made = "2010_2017",
who_made = "i_did",
shipping_template_id = "43862539760"
};
string dataObjJson = JsonConvert.SerializeObject(dataObj, Formatting.Indented);
request.AddBody(dataObjJson);
//request.AddParameter("title", "Test Title");
//request.AddParameter("description", "Test Description");
//request.AddParameter("quantity", "1");
//request.AddParameter("price", "1");
//request.AddParameter("is_supply", "false");
//request.AddParameter("state", "draft");
//request.AddParameter("when_made", "2010_2017");
//request.AddParameter("who_made", "i_did");
//request.AddParameter("shipping_template_id", "43862539760");
var response = _restClient.Execute(request);
return request;
}
产生相同的禁止错误
答案 0 :(得分:0)
您需要在体内传递POST请求。我相信您正在发送格式不正确的帖子请求,因此您收到的回复被禁止。