Phone Icon
Shopify Online Access token to offline token

Converting an Online Shopify Access Token to an Offline Token - PHP (laravel)

There are 2 types of tokens for authentication in shopify apps.

  • Online tokens

  • Offline tokens

In shopify, to make calls outside the admin dashboard, we would need the offline tokens, since online tokens or session tokens expire after a certain time. Offline tokens do not have expiration time, thus, we can use these for calling shopify apis in webhooks, or external scripts. We’ll need to be careful with the offline tokens as authenticated requests can be made using these and these do not expire.

  1. In web.php, where we initialize authorization, add false for an offline mode & true for online mode (false in this case)

    1. Github Iconshopify-api-php/oauth.md at main · Shopify/shopify-api-php

  2. In web.php, in middleware('shopify.auth:offline'); add online/offline as per requirement. (offline in this case)

  3. graphqlProxy in Utils does not give an option for offline tokens, thus we’ll have to implement the function or inherit & override the same.

    1. So instead of

      1. $response = Utils::graphqlProxy($request->header(), $request->cookie(), $request->getContent());

    2. We’ll have,

      1. session = Utils::loadCurrentSession($request->header(), $request->cookie(), false);
                if (!$session) {
                    throw new SessionNotFoundException("Could not find session for GraphQL proxy");
                }

            $client = new Graphql($session->getShop(), $session->getAccessToken());

            $response = $client->proxy($request->getContent());

2022-09-24 17:33:04