Problem access token - authorization code

Hello,

I want to make a new application work in the background
so I want to add a new order information to Shopify just from my website

but I have a problem in the authorization code

I have this error message ‘The authorization code was not found or was already used’

I am building my app like this one tutorial https://help.shopify.com/en/api/tutorials/build-a-shopify-app-with-node-and-express

so I don’t know how I can do that

I need something like how I can get the accessTokenPayload without authorization code
Or how I can get a fixed authorization code

Or how do I run my application on the background without logging in merchants to his store

because the website will be used just by users

Thank you for helping me :slight_smile:

Hey @YoussefAfla ,

It sounds like you’re after offline access: https://help.shopify.com/en/api/getting-started/authentication/oauth/api-access-modes

Offline access tokens are meant for long term access to the store, when no user interaction is involved. This kind of access token is ideal for background work in response to webhooks, or for maintenance work in backgrounded jobs.

1 Like

I too was stuck on this problem. Thanks to your reply. So basically one needs to store access token offline, which means in a database table, for later used by non-user interactions such as webhooks.

1 Like

Hi,

Were you able to find a solution for this issue?

I am facing the same issue

Thanks!

1 Like

@SAJIDMASOOD Hi, if you’re using the React + Node template provided by Shopify, you can go to the index.js file having the express.js code and make the following changes -

// Set up Shopify authentication and webhook handling
app.get(shopify.config.auth.path, shopify.auth.begin())
app.get(
  shopify.config.auth.callbackPath,
  shopify.auth.callback(),
  async (req, res, next) => {
// retrieve your client session
    const client = new shopify.api.clients.Rest({
      session: res.locals.shopify.session,
    })
    const accessToken = client.session.accessToken
    console.log("Token => ", accessToken)

    try {
      saveToMyOwnCustomSolution(accessToken)
    } catch (error) {
      console.error("Error saving access token:", error)
      res.status(500).send("Failed to save token")
    }

    // Continue with the rest of the callback logic
    shopify.redirectToShopifyOrAppRoot()(req, res, next)
  }
)