亚马逊 SP-API 更新库存

时间:2021-07-11 14:09:55

标签: amazon-mws selling-partner-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