C
C
Configure Google Service Account Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthGoogle, which associates an AuthGoogle provider so Chilkat supplies a Google OAuth 2.0 bearer token on each request. The provider is configured with a service account JSON key and the required OAuth scope.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The AuthGoogle provider performs a service-account (server-to-server) OAuth flow. For interactive, browser-based user consent, the OAuth2 provider is used instead.
Chilkat C Downloads
#include <C_CkRest.h>
#include <C_CkAuthGoogle.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkRest rest;
BOOL bTls;
BOOL bAutoReconnect;
HCkAuthGoogle gAuth;
HCkStringBuilder sbJsonKey;
BOOL bLoaded;
const char *jsonKey;
const char *responseText;
success = FALSE;
rest = CkRest_Create();
bTls = TRUE;
bAutoReconnect = TRUE;
success = CkRest_Connect(rest,"example.com",443,bTls,bAutoReconnect);
ERROR: "=" expected
ERROR: Undefined variable(CHECK_SUCCESS)
// Configure Google service-account authentication. Load the service account JSON key and set the
// required OAuth scope(s).
gAuth = CkAuthGoogle_Create();
sbJsonKey = CkStringBuilder_Create();
bLoaded = CkStringBuilder_LoadFile(sbJsonKey,"qa_data/service_account.json");
if (bLoaded != TRUE) {
printf("Failed to load the service account JSON key.\n");
CkRest_Dispose(rest);
CkAuthGoogle_Dispose(gAuth);
CkStringBuilder_Dispose(sbJsonKey);
return;
}
jsonKey = CkStringBuilder_getAsString(sbJsonKey);
if (CkStringBuilder_getLastMethodSuccess(sbJsonKey) == FALSE) {
printf("%s\n",CkStringBuilder_lastErrorText(sbJsonKey));
CkRest_Dispose(rest);
CkAuthGoogle_Dispose(gAuth);
CkStringBuilder_Dispose(sbJsonKey);
return;
}
CkAuthGoogle_putJsonKey(gAuth,jsonKey);
// A space-delimited list of the requested permissions.
CkAuthGoogle_putScope(gAuth,"https://www.googleapis.com/auth/cloud-platform");
// Associate the provider so Chilkat supplies a Google bearer token on each request.
success = CkRest_SetAuthGoogle(rest,gAuth);
ERROR: "=" expected
ERROR: Undefined variable(CHECK_SUCCESS)
responseText = CkRest_fullRequestNoBody(rest,"GET","/storage/v1/b?project=my-project");
ERROR: "=" expected
ERROR: Undefined variable(CHECK_LMS)
printf("%s\n",responseText);
CkRest_Dispose(rest);
CkAuthGoogle_Dispose(gAuth);
CkStringBuilder_Dispose(sbJsonKey);
}