Getting 401 Unauthorized after obtaining Customer Accounts API access token

Getting 401 Unauthorized after obtaining Customer Accounts API access token

brianceleste
Shopify Partner
1 0 0

Hi everyone,

 

I've been trying to set up the headless Customer Accounts API for a client and I've run into some trouble. I've managed to successfully generate an access_token using the /auth/oauth/token endpoint, but when I try and use the token to actually query the API, I get a 401 response saying the token is invalid.

 

I am following the docs here, which say to structure your requests like so:

 

 

 

const response = await fetch(
'https://shopify.com/<shop-id>/account/customer/api/unstable/graphql',
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: {access_token},
    },
    body: JSON.stringify({
      operationName: 'SomeQuery',
      'query { personalAccount { email }}',
      variables: {},
    }),
  },

 

 

 

 

Here is the relevant request I am trying to make:

 

 

 

const fetchCustomer = async (accessToken) => {
    const response = await axios.post(
   `${process.env.NEXT_PUBLIC_CUSTOMER_ACCOUNT_API_URL}/account/customer/api/unstable/graphql`,
      {
        query: `
          query getCustomer {
            customer {
              id
            }
          }
      `,
      },
      {
        headers: {
          'Content-Type': 'application/json',
          Authorization: accessToken,
        },
      }
    );
    const customer = cleanGraphQLResponse(response?.data?.data?.customer);
    setState((prev) => ({ ...prev, customer }));
  };

 

 

 

Replies 3 (3)

st-vietphung
Shopify Partner
1 0 0

I ran into the same issue. 

bootsgridDev
Shopify Partner
22 0 3

How did you get an access token ? 

mikexavier
Shopify Partner
2 0 1

Did you figure this out? I'm running into the same issue.