Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples
Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Get Google API Access Token using P12 Service Account Key

Demonstrates how to get a Google API access token using a P12 service account key.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

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.