Sample code for 30+ languages & platforms
PowerBuilder

Get Google Account Info from OAuth2 Access Token

See more Google APIs Examples

In the Google OAuth2 authorization flow, an application can know the email address of the Google account used to authorize access by requesting and processing user information from the Google UserInfo endpoint after obtaining an access token.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_V_sub
string ls_Name
string ls_Given_name
string ls_Family_name
string ls_Profile
string ls_Picture
string ls_Email
integer li_Email_verified
string ls_Locale

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

// Implements the following CURL command:

// curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://www.googleapis.com/oauth2/v3/userinfo

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

// Adds the "Authorization: Bearer YOUR_ACCESS_TOKEN" header.
loo_Http.AuthToken = "YOUR_ACCESS_TOKEN"

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Http.QuickGetSb("https://www.googleapis.com/oauth2/v3/userinfo",loo_SbResponseBody)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_SbResponseBody
    return
end if

loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Http.LastStatus
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Http.LastHeader
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "sub": "112233445678901234567",
//   "name": "John Doe",
//   "given_name": "John",
//   "family_name": "Doe",
//   "profile": "https://plus.google.com/112233445678901234567",
//   "picture": "https://lh3.googleusercontent.com/a-/AOh14GiU-EXAMPLE",
//   "email": "johndoe@example.com",
//   "email_verified": true,
//   "locale": "en"
// }

// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON

ls_V_sub = loo_JResp.StringOf("sub")
ls_Name = loo_JResp.StringOf("name")
ls_Given_name = loo_JResp.StringOf("given_name")
ls_Family_name = loo_JResp.StringOf("family_name")
ls_Profile = loo_JResp.StringOf("profile")
ls_Picture = loo_JResp.StringOf("picture")
ls_Email = loo_JResp.StringOf("email")
li_Email_verified = loo_JResp.BoolOf("email_verified")
ls_Locale = loo_JResp.StringOf("locale")


destroy loo_Http
destroy loo_SbResponseBody
destroy loo_JResp