PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkAuthGoogle.pb"
IncludeFile "CkRest.pb"
Procedure ChilkatExample()
success.i = 0
rest.i = CkRest::ckCreate()
If rest.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
bTls.i = 1
bAutoReconnect.i = 1
success = CkRest::ckConnect(rest,"example.com",443,bTls,bAutoReconnect)
If success = 0
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
ProcedureReturn
EndIf
; Configure Google service-account authentication. Load the service account JSON key and set the
; required OAuth scope(s).
gAuth.i = CkAuthGoogle::ckCreate()
If gAuth.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
sbJsonKey.i = CkStringBuilder::ckCreate()
If sbJsonKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
bLoaded.i = CkStringBuilder::ckLoadFile(sbJsonKey,"qa_data/service_account.json")
If bLoaded <> 1
Debug "Failed to load the service account JSON key."
CkRest::ckDispose(rest)
CkAuthGoogle::ckDispose(gAuth)
CkStringBuilder::ckDispose(sbJsonKey)
ProcedureReturn
EndIf
jsonKey.s = CkStringBuilder::ckGetAsString(sbJsonKey)
If CkStringBuilder::ckLastMethodSuccess(sbJsonKey) = 0
Debug CkStringBuilder::ckLastErrorText(sbJsonKey)
CkRest::ckDispose(rest)
CkAuthGoogle::ckDispose(gAuth)
CkStringBuilder::ckDispose(sbJsonKey)
ProcedureReturn
EndIf
CkAuthGoogle::setCkJsonKey(gAuth, jsonKey)
; A space-delimited list of the requested permissions.
CkAuthGoogle::setCkScope(gAuth, "https://www.googleapis.com/auth/cloud-platform")
; Associate the provider so Chilkat supplies a Google bearer token on each request.
success = CkRest::ckSetAuthGoogle(rest,gAuth)
If success = 0
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
CkAuthGoogle::ckDispose(gAuth)
CkStringBuilder::ckDispose(sbJsonKey)
ProcedureReturn
EndIf
responseText.s = CkRest::ckFullRequestNoBody(rest,"GET","/storage/v1/b?project=my-project")
If CkRest::ckLastMethodSuccess(rest) = 0
Debug CkRest::ckLastErrorText(rest)
CkRest::ckDispose(rest)
CkAuthGoogle::ckDispose(gAuth)
CkStringBuilder::ckDispose(sbJsonKey)
ProcedureReturn
EndIf
Debug responseText
CkRest::ckDispose(rest)
CkAuthGoogle::ckDispose(gAuth)
CkStringBuilder::ckDispose(sbJsonKey)
ProcedureReturn
EndProcedure