Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nBAutoReconnect
LOCAL oGAuth
LOCAL oSbJsonKey
LOCAL nBLoaded
LOCAL cJsonKey
LOCAL cResponseText
nSuccess := 0
oRest := CreateObject("Chilkat.Rest")
nBTls := 1
nBAutoReconnect := 1
nSuccess := oRest:Connect("example.com", 443, nBTls, nBAutoReconnect)
IF (nSuccess == 0)
? oRest:LastErrorText
oRest:destroy()
RETURN
ENDIF
// Configure Google service-account authentication. Load the service account JSON key and set the
// required OAuth scope(s).
oGAuth := CreateObject("Chilkat.AuthGoogle")
oSbJsonKey := CreateObject("Chilkat.StringBuilder")
nBLoaded := oSbJsonKey:LoadFile("qa_data/service_account.json")
IF (nBLoaded != 1)
? "Failed to load the service account JSON key."
oRest:destroy()
oGAuth:destroy()
oSbJsonKey:destroy()
RETURN
ENDIF
cJsonKey := oSbJsonKey:GetAsString()
IF (oSbJsonKey:LastMethodSuccess == 0)
? oSbJsonKey:LastErrorText
oRest:destroy()
oGAuth:destroy()
oSbJsonKey:destroy()
RETURN
ENDIF
oGAuth:JsonKey := cJsonKey
// A space-delimited list of the requested permissions.
oGAuth:Scope := "https://www.googleapis.com/auth/cloud-platform"
// Associate the provider so Chilkat supplies a Google bearer token on each request.
nSuccess := oRest:SetAuthGoogle(oGAuth)
IF (nSuccess == 0)
? oRest:LastErrorText
oRest:destroy()
oGAuth:destroy()
oSbJsonKey:destroy()
RETURN
ENDIF
cResponseText := oRest:FullRequestNoBody("GET", "/storage/v1/b?project=my-project")
IF (oRest:LastMethodSuccess == 0)
? oRest:LastErrorText
oRest:destroy()
oGAuth:destroy()
oSbJsonKey:destroy()
RETURN
ENDIF
? cResponseText
oRest:destroy()
oGAuth:destroy()
oSbJsonKey:destroy()