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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Files
long li_FileStatus
long li_BackupStatus
oleobject loo_Token
oleobject loo_Client
oleobject loo_Http
oleobject loo_Form
oleobject loo_Resp
oleobject loo_Reply
oleobject loo_Ids
long i
oleobject loo_Url
long h
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Files = create oleobject
li_rc = loo_Files.ConnectToNewObject("Chilkat.FileAccess")
if li_rc < 0 then
destroy loo_Files
MessageBox("Error","Connecting to COM object failed")
return
end if
// Recover the previous complete file if a run stopped during replacement.
li_FileStatus = loo_Files.FileExists3("gmail-token.json")
if li_FileStatus < 0 then
Write-Debug "Could not inspect gmail-token.json."
destroy loo_Files
return
end if
if li_FileStatus = 0 then
li_BackupStatus = loo_Files.FileExists3("gmail-token.json.previous")
if li_BackupStatus < 0 then
Write-Debug "Could not inspect the backup file."
destroy loo_Files
return
end if
if li_BackupStatus = 1 then
li_Success = loo_Files.FileRename("gmail-token.json.previous","gmail-token.json")
if li_Success <> 1 then
Write-Debug "Could not restore the previous complete file."
destroy loo_Files
return
end if
li_FileStatus = 1
end if
end if
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Token.LoadFile("gmail-token.json")
if li_Success <> 1 then
Write-Debug "Could not read gmail-token.json from the private working directory."
destroy loo_Files
destroy loo_Token
return
end if
if (loo_Token.JsonTypeOf("refresh_token") <> 1) OR (loo_Token.StringOfEquals("refresh_token","",1) = 1) then
Write-Debug "Authorize this account for offline access first."
destroy loo_Files
destroy loo_Token
return
end if
loo_Client = create oleobject
li_rc = loo_Client.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Client.LoadFile("gmail-client.json")
if li_Success <> 1 then
Write-Debug "Could not load the private OAuth client file."
destroy loo_Files
destroy loo_Token
destroy loo_Client
return
end if
if (loo_Client.JsonTypeOf("client_id") <> 1) OR (loo_Client.JsonTypeOf("client_secret") <> 1) then
Write-Debug "gmail-client.json must contain client_id and client_secret."
destroy loo_Files
destroy loo_Token
destroy loo_Client
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Form = create oleobject
li_rc = loo_Form.ConnectToNewObject("Chilkat.HttpRequest")
loo_Form.HttpVerb = "POST"
loo_Form.ContentType = "application/x-www-form-urlencoded"
loo_Form.AddParam("grant_type","refresh_token")
loo_Form.AddParam("client_id",loo_Client.StringOf("client_id"))
loo_Form.AddParam("client_secret",loo_Client.StringOf("client_secret"))
loo_Form.AddParam("refresh_token",loo_Token.StringOf("refresh_token"))
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Http.HttpReq("https://oauth2.googleapis.com/token",loo_Form,loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "The token endpoint did not return an HTTP response."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 200) then
Write-Debug "Token refresh failed. HTTP " + string(loo_Resp.StatusCode)
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success = 1 then
if loo_Reply.StringOfEquals("error","invalid_grant",1) = 1 then
Write-Debug "Reconnect this account with interactive authorization."
end if
end if
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Could not parse the refresh response."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
if (loo_Reply.JsonTypeOf("access_token") <> 1) OR (loo_Reply.StringOfEquals("access_token","",1) = 1) then
Write-Debug "Expected access_token in the response."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
// Google usually omits the refresh token. Preserve it unless a replacement is returned.
if (loo_Reply.JsonTypeOf("refresh_token") <> 1) OR (loo_Reply.StringOfEquals("refresh_token","",1) = 1) then
li_Success = loo_Reply.UpdateString("refresh_token",loo_Token.StringOf("refresh_token"))
if li_Success <> 1 then
Write-Debug loo_Reply.LastErrorText
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
end if
if (loo_Reply.HasMember("scope") <> 1) AND (loo_Token.JsonTypeOf("scope") = 1) then
li_Success = loo_Reply.UpdateString("scope",loo_Token.StringOf("scope"))
if li_Success <> 1 then
Write-Debug loo_Reply.LastErrorText
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
end if
// Prepare the complete replacement before moving the last committed file.
li_Success = loo_Reply.WriteFile("gmail-token.json.new")
if li_Success <> 1 then
Write-Debug "Could not write the replacement file."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
if loo_Files.FileExists("gmail-token.json") = 1 then
if loo_Files.FileExists("gmail-token.json.previous") = 1 then
li_Success = loo_Files.FileDelete("gmail-token.json.previous")
if li_Success <> 1 then
Write-Debug "Could not rotate the previous backup."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
end if
li_Success = loo_Files.FileRename("gmail-token.json","gmail-token.json.previous")
if li_Success <> 1 then
Write-Debug "Could not retain the previous complete file."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
end if
li_Success = loo_Files.FileRename("gmail-token.json.new","gmail-token.json")
if li_Success <> 1 then
Write-Debug "Could not commit the replacement. The previous complete file is retained."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
loo_Http.AuthToken = loo_Reply.StringOf("access_token")
li_Success = loo_Http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/messages?labelIds=INBOX&maxResults=10",loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "Gmail did not return an HTTP response."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Gmail HTTP " + string(loo_Resp.StatusCode)
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success = 1 then
Write-Debug loo_Reply.StringOf("error.message")
end if
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a Gmail JSON response."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
return
end if
loo_Ids = create oleobject
li_rc = loo_Ids.ConnectToNewObject("Chilkat.StringTable")
i = 0
do while (i < loo_Reply.SizeOfArray("messages"))
loo_Reply.I = i
li_Success = loo_Ids.Append(loo_Reply.StringOf("messages[i].id"))
if li_Success <> 1 then
Write-Debug loo_Ids.LastErrorText
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
destroy loo_Ids
return
end if
i = i + 1
loop
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
i = 0
do while (i < loo_Ids.Count)
loo_Url.SetString("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
loo_Url.Append(loo_Http.UrlEncode(loo_Ids.StringAt(i)))
loo_Url.Append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject")
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "Gmail did not return an HTTP response."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
destroy loo_Ids
destroy loo_Url
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Gmail HTTP " + string(loo_Resp.StatusCode)
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success = 1 then
Write-Debug loo_Reply.StringOf("error.message")
end if
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
destroy loo_Ids
destroy loo_Url
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a Gmail JSON response."
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
destroy loo_Ids
destroy loo_Url
return
end if
Write-Debug "Message: " + loo_Ids.StringAt(i)
h = 0
do while (h < loo_Reply.SizeOfArray("payload.headers"))
loo_Reply.I = h
Write-Debug loo_Reply.StringOf("payload.headers[i].name") + ": " + loo_Reply.StringOf("payload.headers[i].value")
h = h + 1
loop
i = i + 1
loop
Write-Debug "Inbox messages checked: " + string(loo_Ids.Count)
destroy loo_Files
destroy loo_Token
destroy loo_Client
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
destroy loo_Ids
destroy loo_Url