API error

Solved
Highlighted
Excursionist
12 1 0

Hi I'm trying to run a curl request based on this example https://help.shopify.com/en/api/graphql-admin-api/getting-started

curl -X POST "https://MY_PRIVATE_API_KEY:MY_PRIVATE_API_PASSWORD@MY_STORE.myshopify.com/admin/api/2019-07/graphql.json" -H "Content-Type: application/graphql" -H "X-Shopify-Access-Token: MY_ACCESS_TOKEN" -d '{shop { products(first: 5) {edges {node {id handle } } pageInfo { hasNextPage }}}}'

 

I'm getting {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"} error

When I use https://MY_PRIVATE_API_KEY:MY_PRIVATE_API_PASSWORD@MY_STORE.myshopify.com/admin/products in the browser - I receive the products 

0 Likes
Shopify Staff
Shopify Staff
1489 67 220

Is your app a private app? I don't think you can basic auth a public/unpublished app. In which case, what are you setting the access token to?

 

Additionally, I'm pretty sure you need to structure your curl POST data more like this:

 

curl -X POST "https://shop.myshopify.com/admin/api/2019-07/graphql.json" -H "X-Shopify-Access-Token: ACCESS_TOKEN" -H "Content-Type: application/json" -d '{"query": "query{shop{id}}"}'

Note the encapsulating JSON object with a key called query. That field takes a string value with your GraphQL query, and you can pair it with an optional variables key/value pair if you're using variables.

 

1 Like
Excursionist
12 1 0

It is a private app. I'm using the token from the private app.

When I ran your code with my credentials I still get api error.

curl -X POST "https://key:password@roodyoriginalstest.myshopify.com/admin/api/2019-07/graphql.json" -H "X-Shopify-Access-Token: access token" -H "Content-Type: application/json" -d '{"query": "query{shop{id}}"}'"

0 Likes

Success.

Excursionist
12 1 0

I haven't been able to look at this for a couple of weeks but I've discovered today that the code you gave me for the query works.  The code given in the documentation at https://help.shopify.com/en/api/graphql-admin-api/getting-started regarding CURL POST requests doesn't work.  If I include the "X-Shopify-Access-Token" header with your code it doesn't work.  The query code in the link above also doesn't work.

So this works for me:

curl -X POST "https://API_KEY:PASSWORD@SHOP_NAME.myshopify.com/admin/api/2019-07/graphql.json" -H "Content-Type: application/json" -d '{"query": "query{shop{id}}"}'

0 Likes
Shopify Staff
Shopify Staff
1489 67 220

I didn't even realize this for some reason but it makes sense to me now. The documentation assumes you are using an OAuth access token, and it looks like you were mixing basic auth with the access token authorization approach. I completely missed that, my apologies.

 

It looks like you're using a private app. In which case, yes, the X-Shopify-Access-Token header would cause the request to not work.

 

Cheers.

0 Likes