Sample code for 30+ languages & platforms
PowerBuilder

Get XOAUTH2 Access Token from Google OAuth 2.0 Authorization Server

See more HTTP Examples

Obtains an OAUTH2 access token from the Google OAuth 2.0 Authorization Server. This is for Server to server applications using Google API's that need an access token. See https://developers.google.com/accounts/docs/OAuth2ServiceAccount

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Cert
string ls_Iss
string ls_Scope
string ls_Oauth_sub
integer li_NumSec
string ls_AccessToken

li_Success = 0

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// --------------------------------------------------------------------------------
// 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
// --------------------------------------------------------------------------------
// Begin by loading your Google service account key (.p12)
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("c:/someDirectory/keys/chilkat25-cbd7b42afbd8.p12","notasecret")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Http
    destroy loo_Cert
    return
end if

// The ISS is your service account email address ending in gserviceaccount.com.
ls_Iss = "chilkatsvc@chilkat25.iam.gserviceaccount.com"

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

// The sub is your company email address
ls_Oauth_sub = "bob@yourcompany.com"

// The access token is valid for this number of seconds.
li_NumSec = 3600

ls_AccessToken = loo_Http.G_SvcOauthAccessToken(ls_Iss,ls_Scope,ls_Oauth_sub,li_NumSec,loo_Cert)
if loo_Http.LastMethodSuccess <> 1 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Cert
    return
else
    Write-Debug "access token: " + ls_AccessToken
end if

// The access token allows us to send unlimited emails while it's valid. Once it expires, we must obtain and use a new one.


destroy loo_Http
destroy loo_Cert