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

Unicode C++
#include <CkJsonObjectW.h>
#include <CkOAuth2W.h>

void ChilkatSample(void)
    {
    bool 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;

    CkJsonObjectW tokens;
    success = tokens.LoadFile(L"qa_data/tokens/box_oauth.json");
    if (success != true) {
        wprintf(L"%s\n",tokens.lastErrorText());
        return;
    }

    CkOAuth2W oauth2;
    oauth2.put_TokenEndpoint(L"https://api.box.com/oauth2/token");
    oauth2.put_ClientId(L"BOX_CLIENT_ID");
    oauth2.put_ClientSecret(L"BOX_CLIENT_SECRET");
    oauth2.put_RefreshToken(tokens.stringOf(L"refresh_token"));
    success = oauth2.RefreshAccessToken();
    if (success != true) {
        wprintf(L"%s\n",oauth2.lastErrorText());
        return;
    }

    success = tokens.Load(oauth2.accessTokenResponse());
    if (success != true) {
        wprintf(L"%s\n",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.
    success = tokens.WriteFile(L"qa_data/tokens/box_oauth.json");
    if (success != true) {
        wprintf(L"%s\n",tokens.lastErrorText());
        return;
    }

    wprintf(L"New access and refresh tokens saved. Do not log their values.\n");
    }