![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) Get Google API Access Token using P12 Service Account KeyDemonstrates how to get a Google API access token using a P12 service account key.
Use ChilkatAx-win32.pkg Procedure Test Variant vPfx Handle hoPfx Boolean iSuccess Handle hoGAuth String sIss String sScope String sOauth_sub Integer iNumSec Variant vTlsSock Handle hoTlsSock String sTemp1 // 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 |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.