Unicode C++
Unicode C++
Refresh Access Token on 401 Unauthorized and Retry (Service Account)
See more Google Cloud Storage Examples
Demonstrates how to handle an expired access token error, refresh the token, and retry the request. (In this case, the request is to download an object from Google Cloud Storage.)Chilkat Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkHttpW.h>
#include <CkBinDataW.h>
#include <CkFileAccessW.h>
#include <CkAuthGoogleW.h>
#include <CkSocketW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example uses a previously obtained access token having permission for the
// scope "https://www.googleapis.com/auth/cloud-platform"
// In this example, Get Google Cloud Storage OAuth2 Access Token,
// the service account access token was saved to a text file. This example fetches the access token from the file..
CkStringBuilderW sbToken;
success = sbToken.LoadFile(L"qa_data/tokens/googleCloudStorageAccessToken.txt",L"utf-8");
if (success == false) {
wprintf(L"Failed to load access token.\n");
return;
}
CkHttpW http;
http.put_AuthToken(sbToken.getAsString());
// Construct a URL to download an object named "starfish.jpg" from the "chilkat-ocean" bucket.
http.SetUrlVar(L"bucket_name",L"chilkat-ocean");
http.SetUrlVar(L"object_name",L"starfish.jpg");
const wchar_t *url = L"https://www.googleapis.com/storage/v1/b/{$bucket_name}/o/{$object_name}?alt=media";
// If there is an error response, then we didn't actually download the file data,
// but instead we downloaded an error response..
CkBinDataW fileData;
success = http.DownloadBd(url,fileData);
int responseCode = http.get_LastStatus();
if ((success == false) && (responseCode != 401)) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
if (responseCode == 401) {
wprintf(L"Received 401 Unauthorized response. Attempting to refresh the access token...\n");
// May be that the access token expired.
// Load our JSON key and request a new access token, then retry the original request.
CkFileAccessW fac;
const wchar_t *jsonKey = fac.readEntireTextFile(L"qa_data/googleApi/ChilkatCloud-13a07a2e8b3f.json",L"utf-8");
if (fac.get_LastMethodSuccess() != true) {
wprintf(L"%s\n",fac.lastErrorText());
return;
}
CkAuthGoogleW gAuth;
gAuth.put_JsonKey(jsonKey);
gAuth.put_Scope(L"https://www.googleapis.com/auth/cloud-platform");
gAuth.put_ExpireNumSeconds(3600);
gAuth.put_SubEmailAddress(L"");
CkSocketW tlsSock;
success = tlsSock.Connect(L"www.googleapis.com",443,true,5000);
if (success != true) {
wprintf(L"%s\n",tlsSock.lastErrorText());
return;
}
success = gAuth.ObtainAccessToken(tlsSock);
if (success != true) {
wprintf(L"%s\n",gAuth.lastErrorText());
return;
}
fac.WriteEntireTextFile(L"qa_data/tokens/googleCloudStorageAccessToken.txt",gAuth.accessToken(),L"utf-8",false);
// Retry the original request.
http.put_AuthToken(gAuth.accessToken());
success = http.DownloadBd(url,fileData);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
}
if (responseCode != 200) {
// Get the error response
CkStringBuilderW sbErrorResponse;
sbErrorResponse.AppendBd(fileData,L"utf-8",0,0);
wprintf(L"Error response code = %d\n",responseCode);
wprintf(L"Error:\n");
wprintf(L"%s\n",sbErrorResponse.getAsString());
return;
}
wprintf(L"Success.\n");
// Save the downloaded data to a file.
success = fileData.WriteFile(L"qa_output/starfish.jpg");
}