Unicode C Requires Chilkat v11.5.0+
Unicode C
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
#include <C_CkJsonObjectW.h>
#include <C_CkOAuth2W.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObjectW tokens;
HCkOAuth2W 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 = CkJsonObjectW_Create();
success = CkJsonObjectW_LoadFile(tokens,L"qa_data/tokens/box_oauth.json");
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(tokens));
CkJsonObjectW_Dispose(tokens);
return;
}
oauth2 = CkOAuth2W_Create();
CkOAuth2W_putTokenEndpoint(oauth2,L"https://api.box.com/oauth2/token");
CkOAuth2W_putClientId(oauth2,L"BOX_CLIENT_ID");
CkOAuth2W_putClientSecret(oauth2,L"BOX_CLIENT_SECRET");
CkOAuth2W_putRefreshToken(oauth2,CkJsonObjectW_stringOf(tokens,L"refresh_token"));
success = CkOAuth2W_RefreshAccessToken(oauth2);
if (success != TRUE) {
wprintf(L"%s\n",CkOAuth2W_lastErrorText(oauth2));
CkJsonObjectW_Dispose(tokens);
CkOAuth2W_Dispose(oauth2);
return;
}
success = CkJsonObjectW_Load(tokens,CkOAuth2W_accessTokenResponse(oauth2));
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(tokens));
CkJsonObjectW_Dispose(tokens);
CkOAuth2W_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 = CkJsonObjectW_WriteFile(tokens,L"qa_data/tokens/box_oauth.json");
if (success != TRUE) {
wprintf(L"%s\n",CkJsonObjectW_lastErrorText(tokens));
CkJsonObjectW_Dispose(tokens);
CkOAuth2W_Dispose(oauth2);
return;
}
wprintf(L"New access and refresh tokens saved. Do not log their values.\n");
CkJsonObjectW_Dispose(tokens);
CkOAuth2W_Dispose(oauth2);
}