PowerBuilder
PowerBuilder
Get Google API Access Token using P12 Service Account Key
See more Google APIs Examples
Demonstrates how to get a Google API access token using a P12 service account key.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Pfx
oleobject loo_GAuth
string ls_Iss
string ls_Scope
string ls_Oauth_sub
integer li_NumSec
oleobject loo_TlsSock
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// --------------------------------------------------------------------------------
// For a step-by-step guide for setting up your Google Workspace service account,
// see Setup Google Workspace Account for Sending SMTP GMail from a Service Account
// --------------------------------------------------------------------------------
// First load the PKCS12 (.p12 / .pfx) into a PFX object.
loo_Pfx = create oleobject
li_rc = loo_Pfx.ConnectToNewObject("Chilkat.Pfx")
if li_rc < 0 then
destroy loo_Pfx
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Pfx.LoadPfxFile("qa_data/pfx/chilkat25-cbd7b42afbd8.p12","notasecret")
if li_Success <> 1 then
Write-Debug loo_Pfx.LastErrorText
destroy loo_Pfx
return
end if
loo_GAuth = create oleobject
li_rc = loo_GAuth.ConnectToNewObject("Chilkat.AuthGoogle")
li_Success = loo_GAuth.SetP12(loo_Pfx)
// The ISS is your service account email address ending in gserviceaccount.com.
ls_Iss = "chilkatsvc@chilkat25.iam.gserviceaccount.com"
// The scope is always the following string:
ls_Scope = "https://mail.google.com/"
// The sub is your company email address
ls_Oauth_sub = "info@chilkat.xyz"
// The access token is valid for this number of seconds.
li_NumSec = 3600
loo_GAuth.EmailAddress = ls_Iss
loo_GAuth.Scope = ls_Scope
loo_GAuth.ExpireNumSeconds = li_NumSec
loo_GAuth.SubEmailAddress = ls_Oauth_sub
// Connect to www.googleapis.com
loo_TlsSock = create oleobject
li_rc = loo_TlsSock.ConnectToNewObject("Chilkat.Socket")
li_Success = loo_TlsSock.Connect("www.googleapis.com",443,1,5000)
if li_Success <> 1 then
Write-Debug loo_TlsSock.LastErrorText
destroy loo_Pfx
destroy loo_GAuth
destroy loo_TlsSock
return
end if
// Send the request to obtain the access token.
li_Success = loo_GAuth.ObtainAccessToken(loo_TlsSock)
if li_Success <> 1 then
Write-Debug loo_GAuth.LastErrorText
destroy loo_Pfx
destroy loo_GAuth
destroy loo_TlsSock
return
end if
// Examine the access token:
Write-Debug "Access Token: " + loo_GAuth.AccessToken
destroy loo_Pfx
destroy loo_GAuth
destroy loo_TlsSock