0

有没有人成功地在 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 代码有什么明显的问题吗?

我收到的响应对象上的错误代码是 'Forbidden' 的 'StatusCode' 和 'oauth_problem=signature_invalid...' 的 'Content'

我已经验证我拥有完全权限,并且签名不是我上面提供的 HTTP GET 示例的问题。

附加信息:在没有“数量”参数的情况下发布,我收到一个“BadRequest”错误,指出“预期参数“数量”。

并重新编写代码,所以我将在正文中传递对象:

        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;
    }

导致相同的禁止错误

4

1 回答 1

0

您需要在正文中传递 POST 请求。我相信您正在发送格式错误的发布请求,因此您收到了禁止回复。

于 2018-12-27T17:44:48.187 回答