Cannot get access_token within createShopifyAuth (Koa/Node.js/React.js)

Highlighted
New Member
2 0 0

Hi all, 

I've been following the Node and React Shopify app tutorial, and I wanted to migrate from GraphQL API to REST. As I found out, I need X-Shopify-Access-Token in request header to make calls to Shopify REST API, but I'm confused how to get it from CreateShopifyAuth (I've tried to set access_token to cookie, but there is no cookie-file with my token, shopOrigin cookie has been created as usual).

Here is my code how I create shopify auth and set cookies

server.use(
        createShopifyAuth({
            apiKey: SHOPIFY_API_KEY,
            secret: SHOPIFY_API_SECRET_KEY,
            scopes: ['read_products','write_products','read_script_tags','write_script_tags'],
            afterAuth(ctx){
                const {shop, accessToken} = ctx.session;
                ctx.cookies.set('shopOrigin', shop, {
                    httpOnly: false,
                    secure: true,
                    sameSite: 'none'
                });
                ctx.cookies.set('accessToken', accessToken, {
                    httpOnly: false,
                    secure: true,
                    sameSite: 'none'
                });
                ctx.redirect('/');
            }
        })
    );

I've tried to get my token from accessToken-cookie, but no cookie was created.

Please help me!

0 Likes
Highlighted
New Member
2 0 0

Finally I got access token, I just needed to reinstall app on store, cause createShopifyAuth function works only once upon app installing.
But there appeared another issue: with my token when I try to make API call, I get 401 error: [API] Invalid API key or access token (unrecognized login or wrong password).

Here is my request

let scriptData = await axios.get(`https://${ctx.cookies.get('shopOrigin')}/admin/api/2020-04/script_tags.json`, {
           "X-Shopify-Access-Token": ctx.cookies.get('accessToken')
        });

I've checked my api keys and they are absolutely correct, help me please!

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1040 139 163

Hey @Lil_Budj,

 

You'll need to send the token in the header, try this:

 

let scriptData = await axios.get(`https://${ctx.cookies.get('shopOrigin')}/admin/api/2020-04/script_tags.json`, {
   headers: {
     "X-Shopify-Access-Token": ctx.cookies.get('accessToken')
   }
});

 

0 Likes