Check Gmail from a scheduled job after one-time consent
See more GMail REST API Examples
First run desktop authorization with gmail.readonly and offline access. This applies to personal Gmail and Workspace accounts subject to organization policies. Configure a job to run this code using the same private working directory and user identity. Each run refreshes the token and displays up to ten inbox message headers.
Save the original OAuth client credentials in a private gmail-client.json containing client_id and client_secret. The existing gmail-token.json supplies the refresh token. Use protected credential storage in a deployed application and serialize runs sharing these files. An invalid_grant response requires reconnecting the account. External OAuth apps in Testing normally receive refresh tokens that expire after seven days when Gmail scopes are requested. Google token expiration guidance.
This is a repeatable inbox check. To process each mailbox change across runs, see mailbox synchronization.
Chilkat AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oFiles = ObjCreate("Chilkat.FileAccess")
; Recover the previous complete file if a run stopped during replacement.
Local $iFileStatus = $oFiles.FileExists3("gmail-token.json")
If ($iFileStatus < 0) Then
ConsoleWrite("Could not inspect gmail-token.json." & @CRLF)
Exit
EndIf
If ($iFileStatus = 0) Then
Local $iBackupStatus = $oFiles.FileExists3("gmail-token.json.previous")
If ($iBackupStatus < 0) Then
ConsoleWrite("Could not inspect the backup file." & @CRLF)
Exit
EndIf
If ($iBackupStatus = 1) Then
$bSuccess = $oFiles.FileRename("gmail-token.json.previous","gmail-token.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not restore the previous complete file." & @CRLF)
Exit
EndIf
$iFileStatus = 1
EndIf
EndIf
$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-token.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not read gmail-token.json from the private working directory." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("refresh_token") <> 1) Or ($oToken.StringOfEquals("refresh_token","",True) = True)) Then
ConsoleWrite("Authorize this account for offline access first." & @CRLF)
Exit
EndIf
$oClient = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oClient.LoadFile("gmail-client.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load the private OAuth client file." & @CRLF)
Exit
EndIf
If (($oClient.JsonTypeOf("client_id") <> 1) Or ($oClient.JsonTypeOf("client_secret") <> 1)) Then
ConsoleWrite("gmail-client.json must contain client_id and client_secret." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oForm = ObjCreate("Chilkat.HttpRequest")
$oForm.HttpVerb = "POST"
$oForm.ContentType = "application/x-www-form-urlencoded"
$oForm.AddParam "grant_type","refresh_token"
$oForm.AddParam "client_id",$oClient.StringOf("client_id")
$oForm.AddParam "client_secret",$oClient.StringOf("client_secret")
$oForm.AddParam "refresh_token",$oToken.StringOf("refresh_token")
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oHttp.HttpReq("https://oauth2.googleapis.com/token",$oForm,$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("The token endpoint did not return an HTTP response." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode <> 200)) Then
ConsoleWrite("Token refresh failed. HTTP " & $oResp.StatusCode & @CRLF)
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess = True) Then
If ($oReply.StringOfEquals("error","invalid_grant",True) = True) Then
ConsoleWrite("Reconnect this account with interactive authorization." & @CRLF)
EndIf
EndIf
Exit
EndIf
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Could not parse the refresh response." & @CRLF)
Exit
EndIf
If (($oReply.JsonTypeOf("access_token") <> 1) Or ($oReply.StringOfEquals("access_token","",True) = True)) Then
ConsoleWrite("Expected access_token in the response." & @CRLF)
Exit
EndIf
; Google usually omits the refresh token. Preserve it unless a replacement is returned.
If (($oReply.JsonTypeOf("refresh_token") <> 1) Or ($oReply.StringOfEquals("refresh_token","",True) = True)) Then
$bSuccess = $oReply.UpdateString("refresh_token",$oToken.StringOf("refresh_token"))
If ($bSuccess <> True) Then
ConsoleWrite($oReply.LastErrorText & @CRLF)
Exit
EndIf
EndIf
If (($oReply.HasMember("scope") <> True) And ($oToken.JsonTypeOf("scope") = 1)) Then
$bSuccess = $oReply.UpdateString("scope",$oToken.StringOf("scope"))
If ($bSuccess <> True) Then
ConsoleWrite($oReply.LastErrorText & @CRLF)
Exit
EndIf
EndIf
; Prepare the complete replacement before moving the last committed file.
$bSuccess = $oReply.WriteFile("gmail-token.json.new")
If ($bSuccess <> True) Then
ConsoleWrite("Could not write the replacement file." & @CRLF)
Exit
EndIf
If ($oFiles.FileExists("gmail-token.json") = True) Then
If ($oFiles.FileExists("gmail-token.json.previous") = True) Then
$bSuccess = $oFiles.FileDelete("gmail-token.json.previous")
If ($bSuccess <> True) Then
ConsoleWrite("Could not rotate the previous backup." & @CRLF)
Exit
EndIf
EndIf
$bSuccess = $oFiles.FileRename("gmail-token.json","gmail-token.json.previous")
If ($bSuccess <> True) Then
ConsoleWrite("Could not retain the previous complete file." & @CRLF)
Exit
EndIf
EndIf
$bSuccess = $oFiles.FileRename("gmail-token.json.new","gmail-token.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not commit the replacement. The previous complete file is retained." & @CRLF)
Exit
EndIf
$oHttp.AuthToken = $oReply.StringOf("access_token")
$bSuccess = $oHttp.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/messages?labelIds=INBOX&maxResults=10",$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess = True) Then
ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
Exit
EndIf
$oIds = ObjCreate("Chilkat.StringTable")
Local $i = 0
While ($i < $oReply.SizeOfArray("messages"))
$oReply.I = $i
$bSuccess = $oIds.Append($oReply.StringOf("messages[i].id"))
If ($bSuccess <> True) Then
ConsoleWrite($oIds.LastErrorText & @CRLF)
Exit
EndIf
$i = $i + 1
Wend
$oUrl = ObjCreate("Chilkat.StringBuilder")
$i = 0
While ($i < $oIds.Count)
$oUrl.SetString("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
$oUrl.Append($oHttp.UrlEncode($oIds.StringAt($i)))
$oUrl.Append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject")
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess = True) Then
ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
Exit
EndIf
ConsoleWrite("Message: " & $oIds.StringAt($i) & @CRLF)
Local $iH = 0
While ($iH < $oReply.SizeOfArray("payload.headers"))
$oReply.I = $iH
ConsoleWrite($oReply.StringOf("payload.headers[i].name") & ": " & $oReply.StringOf("payload.headers[i].value") & @CRLF)
$iH = $iH + 1
Wend
$i = $i + 1
Wend
ConsoleWrite("Inbox messages checked: " & $oIds.Count & @CRLF)