Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

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

set http [new_CkHttp]

# 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.
CkHttp_put_AuthToken $http "YOUR_ACCESS_TOKEN"

set sbResponseBody [new_CkStringBuilder]

set success [CkHttp_QuickGetSb $http "https://www.googleapis.com/oauth2/v3/userinfo" $sbResponseBody]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkStringBuilder $sbResponseBody
    exit
}

set jResp [new_CkJsonObject]

CkJsonObject_LoadSb $jResp $sbResponseBody
CkJsonObject_put_EmitCompact $jResp 0

puts "Response Body:"
puts [CkJsonObject_emit $jResp]

set respStatusCode [CkHttp_get_LastStatus $http]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
    puts "Response Header:"
    puts [CkHttp_lastHeader $http]
    puts "Failed."
    delete_CkHttp $http
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jResp
    exit
}

# 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

set v_sub [CkJsonObject_stringOf $jResp "sub"]
set name [CkJsonObject_stringOf $jResp "name"]
set given_name [CkJsonObject_stringOf $jResp "given_name"]
set family_name [CkJsonObject_stringOf $jResp "family_name"]
set profile [CkJsonObject_stringOf $jResp "profile"]
set picture [CkJsonObject_stringOf $jResp "picture"]
set email [CkJsonObject_stringOf $jResp "email"]
set email_verified [CkJsonObject_BoolOf $jResp "email_verified"]
set locale [CkJsonObject_stringOf $jResp "locale"]

delete_CkHttp $http
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp