Missing access token when querying Shopify API from custom app

Solved

Missing access token when querying Shopify API from custom app

wristbands
Shopify Partner
20 3 9

I've gone through the documentation on creating a store-specific custom app, but when I get to the part where I query for the product count, I get the error:

2023-03-16 20:59:46 | backend | /Users/elias/Desktop/zenvy-app/web/node_modules/@shopify/shopify-api/lib/clients/rest/rest_client.js:15
2023-03-16 20:59:46 | backend | throw new ShopifyErrors.MissingRequiredArgument('Missing access token when creating REST client');

 

Here's how I've configured my shopifyApp:

const shopify = shopifyApp({
  api: {
    apiVersion: LATEST_API_VERSION,
    restResources,
    billing: undefined,
  },
  auth: {
    path: "/api/auth",
    callbackPath: "/api/auth/callback",
  },
  webhooks: {
    path: "/api/webhooks",
  },
  sessionStorage: new SQLiteSessionStorage(DB_PATH),
  apiKey: process.env.APP_API_KEY,
  apiVersion: LATEST_API_VERSION,
  apiSecretKey: process.env.ADMIN_API_ACCESS_TOKEN,
  isCustomStoreApp: true,
  scopes: [
    'write_customers',
    'write_products'
  ],
  isEmbeddedApp: false,
  hostName: process.env.HOST_NAME,
  restResources,
});


To get to the above configuration, I first generated the project from npm init @shopify/app@latest app. Then I added additional fields according to the creating a store-specific custom app documentation. As you can see, I have set the apiSecretKey to my Admin API access token (NOT the API secret key), so I'm not sure why it's not working. Also note that I've tried logging all the `process.env` fields, and they are all correctly configured.

 

Also just FYI, I noticed that the documentation is partially out-of-date. The line:

const session = shopify.session.customAppSession("my-shop.myshopify.com");

should be:

const session = shopify.api.session.customAppSession("my-shop.myshopify.com");

And same for the `shopify.rest` commands, where it should be `shopify.api.rest`

 

Anyone know how I can resolve the error I am experiencing?

 

Thanks,

Elias

Accepted Solution (1)
wristbands
Shopify Partner
20 3 9

This is an accepted solution.

Actually, it turns out the problem was the tutorial was meant for instantiating a shopifyApi, not a shopifyApp. To instantiate a shopifyApp, I needed to add the configuration parameters into the api object. Here's how I've now configured my shopifyApp:

 

const shopify = shopifyApp({
  api: {
    apiVersion: LATEST_API_VERSION,
    restResources,
    billing: undefined,
    apiKey: process.env.APP_API_KEY,
    apiSecretKey: process.env.ADMIN_API_ACCESS_TOKEN,
    isCustomStoreApp: true,
    scopes: [
      'write_customers',
      'write_products'
    ],
    isEmbeddedApp: false,
    hostName: process.env.HOST_NAME,  
  },
  auth: {
    path: "/api/auth",
    callbackPath: "/api/auth/callback",
  },
  webhooks: {
    path: "/api/webhooks",
  },
  sessionStorage: new SQLiteSessionStorage(DB_PATH),
});

 

And that's why the documentation had the line correct as the below, since shopify referred to shopifyApi, not shopifyApp:

const session = shopify.session.customAppSession("my-shop.myshopify.com");

View solution in original post

Replies 5 (5)

wristbands
Shopify Partner
20 3 9

I was able to figure it out! It turns out you need to move some of the configuration parameters into the api object. Here's how I've now configured my shopifyApp:

 

const shopify = shopifyApp({
  api: {
    apiVersion: LATEST_API_VERSION,
    restResources,
    billing: undefined,
    isCustomStoreApp: true,
    apiKey: process.env.APP_API_KEY,
    apiSecretKey: process.env.ADMIN_API_ACCESS_TOKEN,
  },
  auth: {
    path: "/api/auth",
    callbackPath: "/api/auth/callback",
  },
  webhooks: {
    path: "/api/webhooks",
  },
  sessionStorage: new SQLiteSessionStorage(DB_PATH),
  scopes: [
    'write_customers',
    'write_products'
  ],
  isEmbeddedApp: false,
  hostName: process.env.HOST_NAME,
});

It would be great if someone could update the documentation to reflect this new required format.

wristbands
Shopify Partner
20 3 9

This is an accepted solution.

Actually, it turns out the problem was the tutorial was meant for instantiating a shopifyApi, not a shopifyApp. To instantiate a shopifyApp, I needed to add the configuration parameters into the api object. Here's how I've now configured my shopifyApp:

 

const shopify = shopifyApp({
  api: {
    apiVersion: LATEST_API_VERSION,
    restResources,
    billing: undefined,
    apiKey: process.env.APP_API_KEY,
    apiSecretKey: process.env.ADMIN_API_ACCESS_TOKEN,
    isCustomStoreApp: true,
    scopes: [
      'write_customers',
      'write_products'
    ],
    isEmbeddedApp: false,
    hostName: process.env.HOST_NAME,  
  },
  auth: {
    path: "/api/auth",
    callbackPath: "/api/auth/callback",
  },
  webhooks: {
    path: "/api/webhooks",
  },
  sessionStorage: new SQLiteSessionStorage(DB_PATH),
});

 

And that's why the documentation had the line correct as the below, since shopify referred to shopifyApi, not shopifyApp:

const session = shopify.session.customAppSession("my-shop.myshopify.com");
Divy_tatva
Shopify Partner
32 1 12

Hello

 

Issue has been resolved using above solution but now my app is not installing any other shop as well as app is not loading in installed store.

 

It's showing session not found error and give shop unauthenticated error.

Thank you

wristbands
Shopify Partner
20 3 9

Hi @Divy_tatva,

Customs apps in Shopify can only be installed on a single store. If you'd like it to be installed on multiple stores, you need to make a public app. Here's more info on Distributing your Shopify app. As for your other errors, sorry I'm not sure what's causing those.

Divy_tatva
Shopify Partner
32 1 12

Hello @wristbands

As I mentioned app is not loading in the installed store, it's showing shop is unauthenticated and this app is a public app,