Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPfx
LOCAL oGAuth
LOCAL cIss
LOCAL cScope
LOCAL cOauth_sub
LOCAL nNumSec
LOCAL oTlsSock
nSuccess := 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.
oPfx := CreateObject("Chilkat.Pfx")
nSuccess := oPfx:LoadPfxFile("qa_data/pfx/chilkat25-cbd7b42afbd8.p12", "notasecret")
IF (nSuccess != 1)
? oPfx:LastErrorText
oPfx:destroy()
RETURN
ENDIF
oGAuth := CreateObject("Chilkat.AuthGoogle")
nSuccess := oGAuth:SetP12(oPfx)
// The ISS is your service account email address ending in gserviceaccount.com.
cIss := "chilkatsvc@chilkat25.iam.gserviceaccount.com"
// The scope is always the following string:
cScope := "https://mail.google.com/"
// The sub is your company email address
cOauth_sub := "info@chilkat.xyz"
// The access token is valid for this number of seconds.
nNumSec := 3600
oGAuth:EmailAddress := cIss
oGAuth:Scope := cScope
oGAuth:ExpireNumSeconds := nNumSec
oGAuth:SubEmailAddress := cOauth_sub
// Connect to www.googleapis.com
oTlsSock := CreateObject("Chilkat.Socket")
nSuccess := oTlsSock:Connect("www.googleapis.com", 443, 1, 5000)
IF (nSuccess != 1)
? oTlsSock:LastErrorText
oPfx:destroy()
oGAuth:destroy()
oTlsSock:destroy()
RETURN
ENDIF
// Send the request to obtain the access token.
nSuccess := oGAuth:ObtainAccessToken(oTlsSock)
IF (nSuccess != 1)
? oGAuth:LastErrorText
oPfx:destroy()
oGAuth:destroy()
oTlsSock:destroy()
RETURN
ENDIF
// Examine the access token:
? "Access Token: " + oGAuth:AccessToken
oPfx:destroy()
oGAuth:destroy()
oTlsSock:destroy()