Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.5.0+

Box: Refresh and Save Rotated OAuth Tokens

See more Box Examples

Refresh the user token saved by the authorization example. Box refresh tokens are single-use and expire after 60 days. Serialize refreshes for a token pair and save the replacement immediately; do not run concurrent refresh jobs.

See the Box API guide for setup and the complete workflow.

Chilkat React Native Downloads

React Native
import { JsonObject, OAuth2 } from '@chilkat/react-native'

async function chilkatExample() {
  // Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
  // See Global Unlock Sample.
  // Guide: Box API Guide.

  const tokens = new JsonObject();
  try {
    tokens.loadFile('qa_data/tokens/box_oauth.json');
  } catch {
    console.log(tokens.lastErrorText);
    return;
  }

  const oauth2 = new OAuth2();
  oauth2.tokenEndpoint = 'https://api.box.com/oauth2/token';
  oauth2.clientId = 'BOX_CLIENT_ID';
  oauth2.clientSecret = 'BOX_CLIENT_SECRET';
  oauth2.refreshToken = tokens.stringOf('refresh_token');
  try {
    await oauth2.refreshAccessTokenAsync();
  } catch {
    console.log(oauth2.lastErrorText);
    return;
  }

  try {
    tokens.load(oauth2.accessTokenResponse);
  } catch {
    console.log(tokens.lastErrorText);
    return;
  }

  // The previous refresh token is now invalid. Preserve this response even if
  // your normal token-store write fails. Do not retry with the old refresh token.
  try {
    tokens.writeFile('qa_data/tokens/box_oauth.json');
  } catch {
    console.log(tokens.lastErrorText);
    return;
  }

  console.log('New access and refresh tokens saved. Do not log their values.');
}