Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set rest [new_CkRest]

set bTls 1
set bAutoReconnect 1
set success [CkRest_Connect $rest "example.com" 443 $bTls $bAutoReconnect]
if {$success == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

#  Configure Google service-account authentication.  Load the service account JSON key and set the
#  required OAuth scope(s).
set gAuth [new_CkAuthGoogle]

set sbJsonKey [new_CkStringBuilder]

set bLoaded [CkStringBuilder_LoadFile $sbJsonKey "qa_data/service_account.json"]
if {$bLoaded != 1} then {
    puts "Failed to load the service account JSON key."
    delete_CkRest $rest
    delete_CkAuthGoogle $gAuth
    delete_CkStringBuilder $sbJsonKey
    exit
}

set jsonKey [CkStringBuilder_getAsString $sbJsonKey]
if {[CkStringBuilder_get_LastMethodSuccess $sbJsonKey] == 0} then {
    puts [CkStringBuilder_lastErrorText $sbJsonKey]
    delete_CkRest $rest
    delete_CkAuthGoogle $gAuth
    delete_CkStringBuilder $sbJsonKey
    exit
}

CkAuthGoogle_put_JsonKey $gAuth $jsonKey

#  A space-delimited list of the requested permissions.
CkAuthGoogle_put_Scope $gAuth "https://www.googleapis.com/auth/cloud-platform"

#  Associate the provider so Chilkat supplies a Google bearer token on each request.
set success [CkRest_SetAuthGoogle $rest $gAuth]
if {$success == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkAuthGoogle $gAuth
    delete_CkStringBuilder $sbJsonKey
    exit
}

set responseText [CkRest_fullRequestNoBody $rest "GET" "/storage/v1/b?project=my-project"]
if {[CkRest_get_LastMethodSuccess $rest] == 0} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkAuthGoogle $gAuth
    delete_CkStringBuilder $sbJsonKey
    exit
}

puts "$responseText"

delete_CkRest $rest
delete_CkAuthGoogle $gAuth
delete_CkStringBuilder $sbJsonKey