Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkSocket.pb"
IncludeFile "CkAuthGoogle.pb"
IncludeFile "CkPfx.pb"

Procedure ChilkatExample()

    success.i = 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.
    pfx.i = CkPfx::ckCreate()
    If pfx.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPfx::ckLoadPfxFile(pfx,"qa_data/pfx/chilkat25-cbd7b42afbd8.p12","notasecret")
    If success <> 1
        Debug CkPfx::ckLastErrorText(pfx)
        CkPfx::ckDispose(pfx)
        ProcedureReturn
    EndIf

    gAuth.i = CkAuthGoogle::ckCreate()
    If gAuth.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkAuthGoogle::ckSetP12(gAuth,pfx)

    ; The ISS is your service account email address ending in gserviceaccount.com.
    iss.s = "chilkatsvc@chilkat25.iam.gserviceaccount.com"

    ; The scope is always the following string:
    scope.s = "https://mail.google.com/"

    ; The sub is your company email address
    oauth_sub.s = "info@chilkat.xyz"

    ; The access token is valid for this number of seconds.
    numSec.i = 3600

    CkAuthGoogle::setCkEmailAddress(gAuth, iss)
    CkAuthGoogle::setCkScope(gAuth, scope)
    CkAuthGoogle::setCkExpireNumSeconds(gAuth, numSec)
    CkAuthGoogle::setCkSubEmailAddress(gAuth, oauth_sub)

    ; Connect to www.googleapis.com
    tlsSock.i = CkSocket::ckCreate()
    If tlsSock.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSocket::ckConnect(tlsSock,"www.googleapis.com",443,1,5000)
    If success <> 1
        Debug CkSocket::ckLastErrorText(tlsSock)
        CkPfx::ckDispose(pfx)
        CkAuthGoogle::ckDispose(gAuth)
        CkSocket::ckDispose(tlsSock)
        ProcedureReturn
    EndIf

    ; Send the request to obtain the access token.
    success = CkAuthGoogle::ckObtainAccessToken(gAuth,tlsSock)
    If success <> 1
        Debug CkAuthGoogle::ckLastErrorText(gAuth)
        CkPfx::ckDispose(pfx)
        CkAuthGoogle::ckDispose(gAuth)
        CkSocket::ckDispose(tlsSock)
        ProcedureReturn
    EndIf

    ; Examine the access token:
    Debug "Access Token: " + CkAuthGoogle::ckAccessToken(gAuth)


    CkPfx::ckDispose(pfx)
    CkAuthGoogle::ckDispose(gAuth)
    CkSocket::ckDispose(tlsSock)


    ProcedureReturn
EndProcedure