Unicode C
Unicode 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 Unicode C Downloads
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkOAuth2W.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
const wchar_t *tokenFilePath;
HCkJsonObjectW jsonToken;
HCkHttpW http;
const wchar_t *jsonResponse;
HCkOAuth2W oauth2;
HCkStringBuilderW sbJson;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
tokenFilePath = L"qa_data/tokens/googleCalendar.json";
// Get our current access token.
jsonToken = CkJsonObjectW_Create();
success = CkJsonObjectW_LoadFile(jsonToken,tokenFilePath);
if (CkJsonObjectW_HasMember(jsonToken,L"access_token") == FALSE) {
wprintf(L"No access token found.\n");
CkJsonObjectW_Dispose(jsonToken);
return;
}
http = CkHttpW_Create();
CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"access_token"));
jsonResponse = CkHttpW_quickGetStr(http,L"https://www.googleapis.com/calendar/v3/users/me/calendarList");
if (CkHttpW_getLastMethodSuccess(http) != TRUE) {
if (CkHttpW_getLastStatus(http) != 401) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
wprintf(L"----\n");
wprintf(L"%s\n",CkHttpW_lastResponseBody(http));
CkJsonObjectW_Dispose(jsonToken);
CkHttpW_Dispose(http);
return;
}
// The access token must've expired.
// Refresh the access token and then retry the request.
oauth2 = CkOAuth2W_Create();
CkOAuth2W_putTokenEndpoint(oauth2,L"https://www.googleapis.com/oauth2/v4/token");
// Replace these with actual values.
CkOAuth2W_putClientId(oauth2,L"GOOGLE-CLIENT-ID");
CkOAuth2W_putClientSecret(oauth2,L"GOOGLE-CLIENT-SECRET");
// Get the "refresh_token"
CkOAuth2W_putRefreshToken(oauth2,CkJsonObjectW_stringOf(jsonToken,L"refresh_token"));
// Send the HTTP POST to refresh the access token..
success = CkOAuth2W_RefreshAccessToken(oauth2);
if (success != TRUE) {
wprintf(L"%s\n",CkOAuth2W_lastErrorText(oauth2));
CkJsonObjectW_Dispose(jsonToken);
CkHttpW_Dispose(http);
CkOAuth2W_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.
CkJsonObjectW_UpdateString(jsonToken,L"access_token",CkOAuth2W_accessToken(oauth2));
// Save the new JSON access token response to a file.
sbJson = CkStringBuilderW_Create();
CkJsonObjectW_putEmitCompact(jsonToken,FALSE);
CkJsonObjectW_EmitSb(jsonToken,sbJson);
CkStringBuilderW_WriteFile(sbJson,tokenFilePath,L"utf-8",FALSE);
wprintf(L"OAuth2 authorization granted!\n");
wprintf(L"New Access Token = %s\n",CkOAuth2W_accessToken(oauth2));
// re-try the original request.
CkHttpW_putAuthToken(http,CkOAuth2W_accessToken(oauth2));
jsonResponse = CkHttpW_quickGetStr(http,L"https://www.googleapis.com/calendar/v3/users/me/calendarList");
if (CkHttpW_getLastMethodSuccess(http) != TRUE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkJsonObjectW_Dispose(jsonToken);
CkHttpW_Dispose(http);
CkOAuth2W_Dispose(oauth2);
CkStringBuilderW_Dispose(sbJson);
return;
}
}
wprintf(L"%s\n",jsonResponse);
wprintf(L"-----------------------------\n");
CkJsonObjectW_Dispose(jsonToken);
CkHttpW_Dispose(http);
CkOAuth2W_Dispose(oauth2);
CkStringBuilderW_Dispose(sbJson);
}