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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oOauth = ObjCreate("Chilkat.OAuth2")
$oOauth.TokenEndpoint = "https://oauth2.googleapis.com/token"
$oOauth.ClientId = "YOUR_DESKTOP_CLIENT_ID"
$oOauth.ClientSecret = "YOUR_DESKTOP_CLIENT_SECRET"
$oOauth.AuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth"
$oOauth.LocalHost = "127.0.0.1"
$oOauth.ListenPort = 55568
$oOauth.ListenPortRangeEnd = 55578
$oOauth.CodeChallenge = True
$oOauth.CodeChallengeMethod = "S256"
$oOauth.Scope = "https://www.googleapis.com/auth/gmail.readonly"
$bSuccess = $oOauth.AddAuthQueryParam("access_type","offline")
If ($bSuccess <> True) Then
ConsoleWrite("Could not configure offline access." & @CRLF)
Exit
EndIf
; This is the interactive setup step. Subsequent refreshes use the saved refresh token.
$bSuccess = $oOauth.AddAuthQueryParam("prompt","consent")
If ($bSuccess <> True) Then
ConsoleWrite("Could not configure consent." & @CRLF)
Exit
EndIf
Local $sAuthUrl = $oOauth.StartAuth()
If ($oOauth.LastMethodSuccess <> True) Then
ConsoleWrite("Could not start the loopback listener." & @CRLF)
Exit
EndIf
$bSuccess = $oOauth.LaunchBrowser($sAuthUrl)
If ($bSuccess <> True) Then
$oOauth.Cancel()
ConsoleWrite("Could not open the system browser." & @CRLF)
Exit
EndIf
Local $iWaited = 0
While (($oOauth.AuthFlowState < 3) And ($iWaited < 180000))
$oOauth.SleepMs 200
$iWaited = $iWaited + 200
Wend
If ($oOauth.AuthFlowState < 3) Then
$oOauth.Cancel()
ConsoleWrite("Authorization timed out." & @CRLF)
Exit
EndIf
If ($oOauth.AuthFlowState <> 3) Then
ConsoleWrite("Authorization did not complete. Flow state: " & $oOauth.AuthFlowState & @CRLF)
Exit
EndIf
$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.Load($oOauth.AccessTokenResponse)
If ($bSuccess <> True) Then
ConsoleWrite("Could not parse the token response." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("refresh_token") <> 1) Or ($oToken.StringOfEquals("refresh_token","",True) = True)) Then
ConsoleWrite("No offline refresh token was issued. The existing cache was preserved." & @CRLF)
Exit
EndIf
$bSuccess = $oToken.WriteFile("gmail-token.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not save the token file." & @CRLF)
Exit
EndIf
ConsoleWrite("Credentials saved in the private working directory." & @CRLF)