Unicode C Requires Chilkat v11.5.0+
Unicode C
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 Unicode C Downloads
#include <C_CkOAuth2W.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkOAuth2W oauth2;
const wchar_t *url;
int elapsedMs;
const wchar_t *accessToken;
const wchar_t *refreshToken;
HCkJsonObjectW 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 = CkOAuth2W_Create();
CkOAuth2W_putListenPort(oauth2,3017);
CkOAuth2W_putAuthorizationEndpoint(oauth2,L"https://account.box.com/api/oauth2/authorize");
CkOAuth2W_putTokenEndpoint(oauth2,L"https://api.box.com/oauth2/token");
CkOAuth2W_putClientId(oauth2,L"BOX_CLIENT_ID");
CkOAuth2W_putClientSecret(oauth2,L"BOX_CLIENT_SECRET");
// Use Box's documented confidential-client authorization-code flow.
CkOAuth2W_putCodeChallenge(oauth2,FALSE);
url = CkOAuth2W_startAuth(oauth2);
if (CkOAuth2W_getLastMethodSuccess(oauth2) != TRUE) {
wprintf(L"%s\n",CkOAuth2W_lastErrorText(oauth2));
CkOAuth2W_Dispose(oauth2);
return;
}
success = CkOAuth2W_LaunchBrowser(oauth2,url);
if (success != TRUE) {
wprintf(L"%s\n",CkOAuth2W_lastErrorText(oauth2));
CkOAuth2W_Dispose(oauth2);
return;
}
elapsedMs = 0;
while ((elapsedMs < 120000) && (CkOAuth2W_getAuthFlowState(oauth2) < 3)) {
CkOAuth2W_SleepMs(oauth2,100);
elapsedMs = elapsedMs + 100;
}
if (CkOAuth2W_getAuthFlowState(oauth2) < 3) {
CkOAuth2W_Cancel(oauth2);
wprintf(L"Authorization timed out.\n");
CkOAuth2W_Dispose(oauth2);
return;
}
if (CkOAuth2W_getAuthFlowState(oauth2) != 3) {
wprintf(L"Authorization was denied or failed. State: %d\n",CkOAuth2W_getAuthFlowState(oauth2));
CkOAuth2W_Dispose(oauth2);
return;
}
// Retain both tokens in your application's protected token store.
// The variables below are deliberately not printed.
accessToken = CkOAuth2W_accessToken(oauth2);
refreshToken = CkOAuth2W_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 = CkJsonObjectW_Create();
success = CkJsonObjectW_Load(tokens,CkOAuth2W_accessTokenResponse(oauth2));
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(tokens));
CkOAuth2W_Dispose(oauth2);
CkJsonObjectW_Dispose(tokens);
return;
}
success = CkJsonObjectW_WriteFile(tokens,L"qa_data/tokens/box_oauth.json");
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(tokens));
CkOAuth2W_Dispose(oauth2);
CkJsonObjectW_Dispose(tokens);
return;
}
wprintf(L"Authorization complete; token response saved in the protected local token file.\n");
CkOAuth2W_Dispose(oauth2);
CkJsonObjectW_Dispose(tokens);
}