Sample code for 30+ languages & platforms
Delphi DLL 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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
tokens: HCkJsonObject;
oauth2: HCkOAuth2;

begin
success := False;

//  Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
//  See Global Unlock Sample.
//  Guide: Box API Guide.

success := False;

tokens := CkJsonObject_Create();
success := CkJsonObject_LoadFile(tokens,'qa_data/tokens/box_oauth.json');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(tokens));
    Exit;
  end;
oauth2 := CkOAuth2_Create();
CkOAuth2_putTokenEndpoint(oauth2,'https://api.box.com/oauth2/token');
CkOAuth2_putClientId(oauth2,'BOX_CLIENT_ID');
CkOAuth2_putClientSecret(oauth2,'BOX_CLIENT_SECRET');
CkOAuth2_putRefreshToken(oauth2,CkJsonObject__stringOf(tokens,'refresh_token'));
success := CkOAuth2_RefreshAccessToken(oauth2);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkOAuth2__lastErrorText(oauth2));
    Exit;
  end;
success := CkJsonObject_Load(tokens,CkOAuth2__accessTokenResponse(oauth2));
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(tokens));
    Exit;
  end;
//  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.
success := CkJsonObject_WriteFile(tokens,'qa_data/tokens/box_oauth.json');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(tokens));
    Exit;
  end;
Memo1.Lines.Add('New access and refresh tokens saved. Do not log their values.');

CkJsonObject_Dispose(tokens);
CkOAuth2_Dispose(oauth2);