Search code examples
pythonamazon-selling-partner-api

'Missing Auth Token' Response when calling Amazon SP-API


I am attempting to get a successful response from the AMZ SP-API. Below is my current 400 response.

I believe the issue is with the headers. The docs here don't really state what the header contents needs to be.

When I use the SaleWeaver package here, I am able to successfully make calls using my AWS and Seller credentials, so I know these work...

FWIW, I am using my 'Merchant Token' as the 'sellerID' PATH value.

import requests
import json

headers = {
  'x-amz-access-token': access,
  'client_id': clientid,
  'client_secret':secret,
  'x-amz-date' : '20220402'
}

payload = {'marketplaceId':'ATVPDKIKX0DER'}

sellerId = merch_token
sku = sku

response = requests.get(
  f'https://sellingpartnerapi-na.amazon.com/listings/2021-08-01/items/{sellerId}/{sku}',
  headers = headers,
  params = payload
)

print(response.text)

response:

{
  "errors": [
    {
      "message": "Access to requested resource is denied.",
     "code": "MissingAuthenticationToken"
    }
  ]
}

Solution

  • The header has 4 fields without signing:

    host: sellingpartnerapi-na.amazon.com
    user-agent: My Selling Tool/2.0 (Language=Java/1.8.0.221;
    Platform=Windows/10)
    x-amz-access-token=Atza|IQEBLjAsAhRmHjNgHpi0U-Dme37rR6CuUpSREXAMPLE
    x-amz-date: 20190430T123600Z
    

    If you use AWS SDK, you don't need to sign your requests. In your case, you'll have to add an Authentication header (which is why it shows missing token right now). With the Auth header, the request looks something like

    Authorization: AWS4-HMAC-SHA256 Credential=AKIAIHV6HIXXXXXXX/20201022/us-east-1/execute-api/aws4_request, SignedHeaders=host;user-agent;x-amz-access-token,
    Signature=5d672d79c15b13162d9279b0855cfba6789a8edb4c82c400e06b5924aEXAMPLE
    host: sellingpartnerapi-na.amazon.com
    user-agent: ...
    x-amz-access-token=Atza|IQEBL...
    x-amz-date: ...
    

    You can read up more on how to calculate the signature and how to add the auth header here.