DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vPfx
Handle hoPfx
Handle hoGAuth
String sIss
String sScope
String sOauth_sub
Integer iNumSec
Variant vTlsSock
Handle hoTlsSock
String sTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatPfx)) To hoPfx
If (Not(IsComObjectCreated(hoPfx))) Begin
Send CreateComObject of hoPfx
End
Get ComLoadPfxFile Of hoPfx "qa_data/pfx/chilkat25-cbd7b42afbd8.p12" "notasecret" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoPfx To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatAuthGoogle)) To hoGAuth
If (Not(IsComObjectCreated(hoGAuth))) Begin
Send CreateComObject of hoGAuth
End
Get pvComObject of hoPfx to vPfx
Get ComSetP12 Of hoGAuth vPfx To iSuccess
// The ISS is your service account email address ending in gserviceaccount.com.
Move "chilkatsvc@chilkat25.iam.gserviceaccount.com" To sIss
// The scope is always the following string:
Move "https://mail.google.com/" To sScope
// The sub is your company email address
Move "info@chilkat.xyz" To sOauth_sub
// The access token is valid for this number of seconds.
Move 3600 To iNumSec
Set ComEmailAddress Of hoGAuth To sIss
Set ComScope Of hoGAuth To sScope
Set ComExpireNumSeconds Of hoGAuth To iNumSec
Set ComSubEmailAddress Of hoGAuth To sOauth_sub
// Connect to www.googleapis.com
Get Create (RefClass(cComChilkatSocket)) To hoTlsSock
If (Not(IsComObjectCreated(hoTlsSock))) Begin
Send CreateComObject of hoTlsSock
End
Get ComConnect Of hoTlsSock "www.googleapis.com" 443 True 5000 To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoTlsSock To sTemp1
Showln sTemp1
Procedure_Return
End
// Send the request to obtain the access token.
Get pvComObject of hoTlsSock to vTlsSock
Get ComObtainAccessToken Of hoGAuth vTlsSock To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoGAuth To sTemp1
Showln sTemp1
Procedure_Return
End
// Examine the access token:
Get ComAccessToken Of hoGAuth To sTemp1
Showln "Access Token: " sTemp1
End_Procedure