亚马逊 SP-API 更新库存

问题描述

我正在尝试使用 SP-API 更新我的库存 Amazon Catalog 的库存。 这是我的代码

  private static IRestRequest CreateRequestFeedDocument()
   {
      IRestRequest restRequest = new RestRequest("Feeds/2020-09-04/documents",Method.POST);

      AmazonInventory obj = new AmazonInventory();
      obj.SKU = "GD4297-44";
      obj.Available = "true";
      obj.Quantity = "20";
      string xml = string.Empty;

      using (var stringwriter = new System.IO.StringWriter())
      {
         var serializer = new XmlSerializer(obj.GetType());
         serializer.Serialize(stringwriter,obj);
         xml = stringwriter.ToString();
      }

      restRequest.AddParameter("contentType",xml,ParameterType.RequestBody);
      restRequest.AddQueryParameter("MarketplaceIds","APJ6JRA9NG5V4,A1PA6795UKMFR9,A13V1IB3VIYZZH,A1805IZSGTT6HS,A1F83G8C2ARO7P,A1RKKUPIHCS9HS,A2NODRKZP88ZB9");


      AssumeRoleResponse assumeRoleResponse = null;

      Task.Run(async () =>
      {
         assumeRoleResponse = await GetAssumeRoletokenDetail();
      }).GetAwaiter().GetResult();

      // LWA credentials for app in amazon seller central
      var clientId = AmazonCredentials.Default.ClientId;
      var clientSecret = AmazonCredentials.Default.ClientSecret;
      // generate refresh token with 'Authorize' action for app in amazon seller central
      var refreshToken = AmazonCredentials.Default.RefreshToken;

      var lwaAuthCreds = new LWAAuthorizationCredentials
      {
         ClientId = clientId,ClientSecret = clientSecret,RefreshToken = refreshToken,Endpoint = new Uri("https://api.amazon.com/auth/o2/token")
      };

      restRequest = new LWAAuthorizationSigner(lwaAuthCreds).Sign(restRequest);

      var awsAuthCreds = new AWSAuthenticationCredentials
      {
         AccessKeyId = assumeRoleResponse.Credentials.AccessKeyId,SecretKey = assumeRoleResponse.Credentials.SecretAccessKey,Region = AmazonCredentials.Default.Region
      };

      restRequest.AddHeader("x-amz-security-token",assumeRoleResponse.Credentials.SessionToken);

      restRequest = new AWSsigv4Signer(awsAuthCreds)
         .Sign(restRequest,restClient.BaseUrl.Host);

      return restRequest;
   }

restClient = new RestClient(live_url_base);
      IRestRequest restRequest = CreateRequestFeedDocument();
      var response = restClient.Execute(restRequest);

我收到以下错误

{
  "errors": [
   { 
      "code": "InvalidInput","message": "Invalid Input","details": ""
    }
   ]
}

我在网上找到了这个解决方

添加这一行:

      restRequest.AddJsonBody(new { contentType = "text/xml; charset=UTF-8" });

我没有收到 Invalid inout 错误,但我收到了

<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>

[![在此处输入图片描述][1]][1]

我错在哪里?

谢谢 [1]:https://i.stack.imgur.com/kw5JC.png

解决方法

这是亚马逊的新公告。

https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/use-case-guides/feeds-api-use-case-guide/feeds-api-use-case-guide_2021-06-30.md#step-3-upload-the-feed-data

完美运行!

https://github.com/clousale/amazon-sp-api-php/issues/61#issuecomment-825542099