Authorize a desktop application to use Gmail
See more GMail REST API Examples
Enable the Gmail API, configure the OAuth consent screen, and create a Desktop app OAuth client. Launch the system browser and receive the authorization code on a loopback listener. The sample requests read-only access with PKCE and offline access. For another operation, select the scope in that example before authorizing. Gmail scopes.
Run in a private, user-only directory. The file contains bearer credentials. In a deployed application use protected credential storage. To reuse an existing grant, copy the older examples’ qa_data/tokens/gmail.json to gmail-token.json in this private directory. The token retains its original scopes. Gmail clients using IMAP, POP, or SMTP with OAuth need the https://mail.google.com/ scope. Use a separate cache for each user and client.
Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set oauth = CreateObject("Chilkat.OAuth2")
oauth.TokenEndpoint = "https://oauth2.googleapis.com/token"
oauth.ClientId = "YOUR_DESKTOP_CLIENT_ID"
oauth.ClientSecret = "YOUR_DESKTOP_CLIENT_SECRET"
oauth.AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth"
oauth.LocalHost = "127.0.0.1"
oauth.ListenPort = 55568
oauth.ListenPortRangeEnd = 55578
oauth.CodeChallenge = 1
oauth.CodeChallengeMethod = "S256"
oauth.Scope = "https://www.googleapis.com/auth/gmail.readonly"
success = oauth.AddAuthQueryParam("access_type","offline")
If (success <> 1) Then
outFile.WriteLine("Could not configure offline access.")
WScript.Quit
End If
' This is the interactive setup step. Subsequent refreshes use the saved refresh token.
success = oauth.AddAuthQueryParam("prompt","consent")
If (success <> 1) Then
outFile.WriteLine("Could not configure consent.")
WScript.Quit
End If
authUrl = oauth.StartAuth()
If (oauth.LastMethodSuccess <> 1) Then
outFile.WriteLine("Could not start the loopback listener.")
WScript.Quit
End If
success = oauth.LaunchBrowser(authUrl)
If (success <> 1) Then
success = oauth.Cancel()
outFile.WriteLine("Could not open the system browser.")
WScript.Quit
End If
waited = 0
Do While ((oauth.AuthFlowState < 3) And (waited < 180000))
oauth.SleepMs 200
waited = waited + 200
Loop
If (oauth.AuthFlowState < 3) Then
success = oauth.Cancel()
outFile.WriteLine("Authorization timed out.")
WScript.Quit
End If
If (oauth.AuthFlowState <> 3) Then
outFile.WriteLine("Authorization did not complete. Flow state: " & oauth.AuthFlowState)
WScript.Quit
End If
set token = CreateObject("Chilkat.JsonObject")
success = token.Load(oauth.AccessTokenResponse)
If (success <> 1) Then
outFile.WriteLine("Could not parse the token response.")
WScript.Quit
End If
If ((token.JsonTypeOf("refresh_token") <> 1) Or (token.StringOfEquals("refresh_token","",1) = 1)) Then
outFile.WriteLine("No offline refresh token was issued. The existing cache was preserved.")
WScript.Quit
End If
success = token.WriteFile("gmail-token.json")
If (success <> 1) Then
outFile.WriteLine("Could not save the token file.")
WScript.Quit
End If
outFile.WriteLine("Credentials saved in the private working directory.")
outFile.Close