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

C
#include <C_CkJsonObject.h>
#include <C_CkOAuth2.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject tokens;
    HCkOAuth2 oauth2;

    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) {
        printf("%s\n",CkJsonObject_lastErrorText(tokens));
        CkJsonObject_Dispose(tokens);
        return;
    }

    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) {
        printf("%s\n",CkOAuth2_lastErrorText(oauth2));
        CkJsonObject_Dispose(tokens);
        CkOAuth2_Dispose(oauth2);
        return;
    }

    success = CkJsonObject_Load(tokens,CkOAuth2_accessTokenResponse(oauth2));
    if (success != TRUE) {
        printf("%s\n",CkJsonObject_lastErrorText(tokens));
        CkJsonObject_Dispose(tokens);
        CkOAuth2_Dispose(oauth2);
        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 = CkJsonObject_WriteFile(tokens,"qa_data/tokens/box_oauth.json");
    if (success != TRUE) {
        printf("%s\n",CkJsonObject_lastErrorText(tokens));
        CkJsonObject_Dispose(tokens);
        CkOAuth2_Dispose(oauth2);
        return;
    }

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


    CkJsonObject_Dispose(tokens);
    CkOAuth2_Dispose(oauth2);

    }