C
C
Automatically Refresh Token for 401 Unauthorized
See more Google Calendar Examples
Demonstrates how to automatically refresh an access token (without user interaction) when the token expires and a 401 Unauthorized response is received.Chilkat C Downloads
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkOAuth2.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
const char *tokenFilePath;
HCkJsonObject jsonToken;
HCkHttp http;
const char *jsonResponse;
HCkOAuth2 oauth2;
HCkStringBuilder sbJson;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
tokenFilePath = "qa_data/tokens/googleCalendar.json";
// Get our current access token.
jsonToken = CkJsonObject_Create();
success = CkJsonObject_LoadFile(jsonToken,tokenFilePath);
if (CkJsonObject_HasMember(jsonToken,"access_token") == FALSE) {
printf("No access token found.\n");
CkJsonObject_Dispose(jsonToken);
return;
}
http = CkHttp_Create();
CkHttp_putAuthToken(http,CkJsonObject_stringOf(jsonToken,"access_token"));
jsonResponse = CkHttp_quickGetStr(http,"https://www.googleapis.com/calendar/v3/users/me/calendarList");
if (CkHttp_getLastMethodSuccess(http) != TRUE) {
if (CkHttp_getLastStatus(http) != 401) {
printf("%s\n",CkHttp_lastErrorText(http));
printf("----\n");
printf("%s\n",CkHttp_lastResponseBody(http));
CkJsonObject_Dispose(jsonToken);
CkHttp_Dispose(http);
return;
}
// The access token must've expired.
// Refresh the access token and then retry the request.
oauth2 = CkOAuth2_Create();
CkOAuth2_putTokenEndpoint(oauth2,"https://www.googleapis.com/oauth2/v4/token");
// Replace these with actual values.
CkOAuth2_putClientId(oauth2,"GOOGLE-CLIENT-ID");
CkOAuth2_putClientSecret(oauth2,"GOOGLE-CLIENT-SECRET");
// Get the "refresh_token"
CkOAuth2_putRefreshToken(oauth2,CkJsonObject_stringOf(jsonToken,"refresh_token"));
// Send the HTTP POST to refresh the access token..
success = CkOAuth2_RefreshAccessToken(oauth2);
if (success != TRUE) {
printf("%s\n",CkOAuth2_lastErrorText(oauth2));
CkJsonObject_Dispose(jsonToken);
CkHttp_Dispose(http);
CkOAuth2_Dispose(oauth2);
return;
}
// The response contains a new access token, but we must keep
// our existing refresh token for when we need to refresh again in the future.
CkJsonObject_UpdateString(jsonToken,"access_token",CkOAuth2_accessToken(oauth2));
// Save the new JSON access token response to a file.
sbJson = CkStringBuilder_Create();
CkJsonObject_putEmitCompact(jsonToken,FALSE);
CkJsonObject_EmitSb(jsonToken,sbJson);
CkStringBuilder_WriteFile(sbJson,tokenFilePath,"utf-8",FALSE);
printf("OAuth2 authorization granted!\n");
printf("New Access Token = %s\n",CkOAuth2_accessToken(oauth2));
// re-try the original request.
CkHttp_putAuthToken(http,CkOAuth2_accessToken(oauth2));
jsonResponse = CkHttp_quickGetStr(http,"https://www.googleapis.com/calendar/v3/users/me/calendarList");
if (CkHttp_getLastMethodSuccess(http) != TRUE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkJsonObject_Dispose(jsonToken);
CkHttp_Dispose(http);
CkOAuth2_Dispose(oauth2);
CkStringBuilder_Dispose(sbJson);
return;
}
}
printf("%s\n",jsonResponse);
printf("-----------------------------\n");
CkJsonObject_Dispose(jsonToken);
CkHttp_Dispose(http);
CkOAuth2_Dispose(oauth2);
CkStringBuilder_Dispose(sbJson);
}