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

Box: Authorize a User with OAuth 2.0

See more Box Examples

Interactive local-development example using a browser and a loopback callback. Register exactly http://localhost:3017/ in the Box User app. Run this in a trusted environment that can hold the client secret. A deployed web app must handle its registered HTTPS callback on the server.

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

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkOAuth2 oauth2;
    const char *url;
    int elapsedMs;
    const char *accessToken;
    const char *refreshToken;
    HCkJsonObject tokens;

    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;

    oauth2 = CkOAuth2_Create();
    CkOAuth2_putListenPort(oauth2,3017);
    CkOAuth2_putAuthorizationEndpoint(oauth2,"https://account.box.com/api/oauth2/authorize");
    CkOAuth2_putTokenEndpoint(oauth2,"https://api.box.com/oauth2/token");
    CkOAuth2_putClientId(oauth2,"BOX_CLIENT_ID");
    CkOAuth2_putClientSecret(oauth2,"BOX_CLIENT_SECRET");
    //  Use Box's documented confidential-client authorization-code flow.
    CkOAuth2_putCodeChallenge(oauth2,FALSE);
    url = CkOAuth2_startAuth(oauth2);
    if (CkOAuth2_getLastMethodSuccess(oauth2) != TRUE) {
        printf("%s\n",CkOAuth2_lastErrorText(oauth2));
        CkOAuth2_Dispose(oauth2);
        return;
    }

    success = CkOAuth2_LaunchBrowser(oauth2,url);
    if (success != TRUE) {
        printf("%s\n",CkOAuth2_lastErrorText(oauth2));
        CkOAuth2_Dispose(oauth2);
        return;
    }

    elapsedMs = 0;
    while ((elapsedMs < 120000) && (CkOAuth2_getAuthFlowState(oauth2) < 3)) {
        CkOAuth2_SleepMs(oauth2,100);
        elapsedMs = elapsedMs + 100;
    }

    if (CkOAuth2_getAuthFlowState(oauth2) < 3) {
        CkOAuth2_Cancel(oauth2);
        printf("Authorization timed out.\n");
        CkOAuth2_Dispose(oauth2);
        return;
    }

    if (CkOAuth2_getAuthFlowState(oauth2) != 3) {
        printf("Authorization was denied or failed. State: %d\n",CkOAuth2_getAuthFlowState(oauth2));
        CkOAuth2_Dispose(oauth2);
        return;
    }

    //  Retain both tokens in your application's protected token store.
    //  The variables below are deliberately not printed.
    accessToken = CkOAuth2_accessToken(oauth2);
    refreshToken = CkOAuth2_refreshToken(oauth2);
    //  For this local-development example only, save in a directory restricted to this user.
    //  Create qa_data/tokens before running; never put this file under a web root.
    tokens = CkJsonObject_Create();
    success = CkJsonObject_Load(tokens,CkOAuth2_accessTokenResponse(oauth2));
    if (success != TRUE) {
        printf("%s\n",CkJsonObject_lastErrorText(tokens));
        CkOAuth2_Dispose(oauth2);
        CkJsonObject_Dispose(tokens);
        return;
    }

    success = CkJsonObject_WriteFile(tokens,"qa_data/tokens/box_oauth.json");
    if (success != TRUE) {
        printf("%s\n",CkJsonObject_lastErrorText(tokens));
        CkOAuth2_Dispose(oauth2);
        CkJsonObject_Dispose(tokens);
        return;
    }

    printf("Authorization complete; token response saved in the protected local token file.\n");


    CkOAuth2_Dispose(oauth2);
    CkJsonObject_Dispose(tokens);

    }