Code Cookbook

Renew OAuth Token

Automatically renew OAuth tokens to keep them from expiring.

Server Side
Client Side

OAuth tokens need to be renewed regularly because they expire after 30 days.

Before you start

To use the example code as written, you will need:

Renew an OAuth token

To renew an OAuth token, send a request to the RenewToken endpoint. OAuth tokens expire 30 days after they are issued. You can renew OAuth tokens any time from 24 hours after its issued to 15 days after they expire.

Add a function that creates a renew token request body:

// Renew OAuth token.
function renewOAuthToken($oauthToken) {

  // Create an OAuth API client
  $oauthApi = new SquareConnect\Api\OAuthApi($defaultApiClient);
  $body = new \SquareConnect\Model\RenewTokenRequest();

  // Set the POST body
  $body->setAccessToken($oauthToken);

  try {
      $result = $oauthApi->renewToken($body);
  } catch (Exception $e) {
      error_log 'Exception when calling OAuthApi->renewToken: ' . $e->getMessage();
      throw new Exception("Error Processing Request: Token renewal failed!", 1);
  }

  return $result->getAccessToken();
}

If your request succeeds, the RenewToken endpoint returns a renewed OAuth token. The renew function can be called at any point in your normal workflow to maintain the associated OAuth token.

Prev
< Browse More Recipes
Next
Browse More OAuth Recipes >

Contact Developer Support, join our Slack channel, or ask for help on Stack Overflow