PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_BAutoReconnect
oleobject loo_GAuth
oleobject loo_SbJsonKey
integer li_BLoaded
string ls_JsonKey
string ls_ResponseText
li_Success = 0
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
destroy loo_Rest
MessageBox("Error","Connecting to COM object failed")
return
end if
li_BTls = 1
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("example.com",443,li_BTls,li_BAutoReconnect)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
return
end if
// Configure Google service-account authentication. Load the service account JSON key and set the
// required OAuth scope(s).
loo_GAuth = create oleobject
li_rc = loo_GAuth.ConnectToNewObject("Chilkat.AuthGoogle")
loo_SbJsonKey = create oleobject
li_rc = loo_SbJsonKey.ConnectToNewObject("Chilkat.StringBuilder")
li_BLoaded = loo_SbJsonKey.LoadFile("qa_data/service_account.json")
if li_BLoaded <> 1 then
Write-Debug "Failed to load the service account JSON key."
destroy loo_Rest
destroy loo_GAuth
destroy loo_SbJsonKey
return
end if
ls_JsonKey = loo_SbJsonKey.GetAsString()
if loo_SbJsonKey.LastMethodSuccess = 0 then
Write-Debug loo_SbJsonKey.LastErrorText
destroy loo_Rest
destroy loo_GAuth
destroy loo_SbJsonKey
return
end if
loo_GAuth.JsonKey = ls_JsonKey
// A space-delimited list of the requested permissions.
loo_GAuth.Scope = "https://www.googleapis.com/auth/cloud-platform"
// Associate the provider so Chilkat supplies a Google bearer token on each request.
li_Success = loo_Rest.SetAuthGoogle(loo_GAuth)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
destroy loo_GAuth
destroy loo_SbJsonKey
return
end if
ls_ResponseText = loo_Rest.FullRequestNoBody("GET","/storage/v1/b?project=my-project")
if loo_Rest.LastMethodSuccess = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
destroy loo_GAuth
destroy loo_SbJsonKey
return
end if
Write-Debug ls_ResponseText
destroy loo_Rest
destroy loo_GAuth
destroy loo_SbJsonKey