Authentication only works sometimes Shopify Public App

NewToAppDevHere
Shopify Partner
2 0 0

Hi,

 

I'm building a Shopify app that in this method sends a request to my backend which uses shopify API to fetch products. I get the Shopify API key like this in my Shopify Remix app

const { session } = await authenticate.admin(request);
const response = await axios.post(
`${API_DOMAIN}/products/get`,
{
api_key: session.accessToken,
store_url: storeURL
},
{
headers: {
'Content-Type': 'application/json'
}
}
);

From my backend i send this request:

@shopify_get_products_route.route('my/route', methods=['POST'])
def shopify_get_products():
    print("initiating get_all_products")
    data = request.get_json()
    api_key = data['api_key']
    shop_url = data["store_url"]
    shop_url = shop_url.replace('https://', '')
    shop_name = shop_url.split('.')[0]
    headers = {
        "Content-Type": "application/json",
        "X-Shopify-Access-Token": api_key
    }

    products = []
    since_id = 0

    while True:
        url = f"https://{shop_url}/admin/api/2024-04/products.json?since_id={since_id}&limit=250"
        print("URL: ", url)
        response = requests.get(url, headers=headers)
        print("Response:", response.text)
        if response.status_code == 200:
            new_products = response.json()["products"]
            if not new_products:
                break
            products.extend(new_products)
            print(f"Retrieved {len(new_products)} new products, total products: {len(products)}")
            new_products.sort(key=lambda product: product['id'])
            since_id = new_products[-1]['id']
        else:
            print(f"Failed to retrieve products. Status code: {response.status_code}")
            return jsonify({'error': "error", 'response': 'Failed to retrieve products'}), response.status_code

The issue I'm running into is that of my two development stores, only one correctly fetches products, while the other gets this error: Response: {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"} Failed to retrieve products. Status code: 401

 

They both fetch the API key exactly the same methods, and I am getting two different shop url and api keys. Am i somehow fetching the API key incorrectly?

 

Replies 0 (0)