user1636588
user1636588

Reputation: 101

Walmart Oder API call not working, Gives UNAUTHORIZED.GMP_GATEWAY_API

I am trying to access the Walmart Orders API and am trying to generate a token to do so. I've written this code:

import requests
import base64

# Replace with your API key and secret key
api_key = "AAA"
secret_key = "BBB"

key = api_key + ':' + secret_key
encoded_bytes = base64.b64encode(key.encode("utf-8"))
encoded_str = encoded_bytes.decode("utf-8")


# Walmart OAuth2.0 token endpoint
token_endpoint = "https://sandbox.walmartapis.com/v3/token"
headers = {
    'WM_SVC.NAME': 'Walmart Marketplace',
    'Authorization': 'Basic ' + encoded_str,
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'WM_SVC.VERSION': '1.0.0',
    'WM_QOS.CORRELATION_ID': '76080106-eaf2-4ec1-a7dd-ffa0473182c5',
    'grant_type': 'client_credentials'}


# Define the OAuth2.0 client credentials grant request parameters
grant_type = "client_credentials"
scope = "https://marketplace.walmartapis.com/v3/orders.readonly"

# Send the OAuth2.0 client credentials grant request to the token endpoint
response = requests.post(
    token_endpoint,
    headers=headers
)

# Check if the request was successful
if response.status_code == 200:
    # Extract the access token from the response
    access_token = response.json()["access_token"]
    # Use the access token to authenticate requests to the Order Management API
    # ...
else:
    print("Failed to get access token:", response.text)

But get the following error:

Failed to get access token: {"error":[{"code":"UNAUTHORIZED.GMP_GATEWAY_API","field":"UNAUTHORIZED","description":"Unauthorized","info":"Unauthorized","severity":"ERROR","category":"DATA","causes":[],"errorIdentifiers":{}}]}

Can anyone tell me what I might be doing wrong?

Token API documentation: https://developer.walmart.com/api/us/mp/auth#operation/tokenAPI Authentication documentation: https://developer.walmart.com/doc/us/mp/us-mp-auth/

Upvotes: 0

Views: 750

Answers (1)

toastifer
toastifer

Reputation: 539

The grant-type needs to be sent in the request BODY, not the header. It should be application/x-www-form-urlencoded

Upvotes: 2

Related Questions