Retrieving orders using Python requests.get

Solved
Velor-cycling
Tourist
4 1 0

Hi,

 

I'm trying to retrieve orders from my Shopify webshop using Python.

I set up the app in the admin page and retrieved API admin key. I verified that the key works because the following code is allowing me to get order information.

api_session = shopify.Session(shop_url, '2023-01', api_admin)
shopify.ShopifyResource.activate_session(api_session)

 

However, the following code is giving me a 401 error. Can someone explain what I am doing wrong?

 

hostname='my-shop.myshopify.com'
url = f"https://{api_admin}@{hostname}/admin/api/2023-04/orders.json?limit=250&fulfillment_status=all"
response = requests.request("GET", url)
print(response)
 
Thanks!
Accepted Solution (1)
Velor-cycling
Tourist
4 1 0

This is an accepted solution.

OK I did something wrong passing the authentication token to the requests.get command.

This seems to be working now, no error 401:

 

headers = {'X-Shopify-Access-Token': api_admin, 'Content-Type': 'application/json'}
response = requests.get(url, headers=headers)

View solution in original post

Replies 3 (3)
Velor-cycling
Tourist
4 1 0

Same thing if I try this:

headers = {'X-Shopify-Access-Token': api_admin}
response = requests.get(url, headers)
print(response)
 
any pointers as to what I'm doing wrong are welcome!
Velor-cycling
Tourist
4 1 0

This is an accepted solution.

OK I did something wrong passing the authentication token to the requests.get command.

This seems to be working now, no error 401:

 

headers = {'X-Shopify-Access-Token': api_admin, 'Content-Type': 'application/json'}
response = requests.get(url, headers=headers)
jacobhales
Visitor
1 0 0
 

The 401 error indicates that you are not authorized to access the resource. This could be because you are using the wrong API key, or because you are not sending the API key in the correct way.

To fix this error, try the following:

  1. Make sure that you are using the correct API key. You can find your API key in the Shopify admin panel under Apps and sales channels > Apps.
  2. Make sure that you are sending the API key in the correct header. The API key should be sent in the X-Shopify-Access-Token header.

Here is an example of how to retrieve orders from Shopify using Python:

 

import requests

# Your Shopify shop URL
shop_url = 'my-shop.myshopify.com'

# Your Shopify API admin key
api_admin = 'your-api-admin-key'

# Build the request URL
url = f'https://{shop_url}/admin/api/2023-04/orders.json?limit=250&fulfillment_status=all'

# Set the request headers
headers = {
    'X-Shopify-Access-Token': api_admin
}

# Make the request
response = requests.get(url, headers=headers)

# Check the response status code
if response.status_code == 200:
    # Success!
    orders = response.json()
else:
    # Error!
    print(response.status_code)
    print(response.content)

 

 If you are still getting a 401 error after trying the above, please contact Shopify support for assistance.