PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
Read selected headers for a page of Gmail messages
See more GMail REST API Examples
List up to five messages and fetch only From, Subject, and Date for each one. Request metadataHeaders once per header name. Read the returned headers by name because order and presence vary. This header preview leaves the MIME body on Gmail.
Authorize the application with https://www.googleapis.com/auth/gmail.metadata. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Reply
oleobject loo_Ids
long li_Count
long i
oleobject loo_Url
long h
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Token
MessageBox("Error","Connecting to COM object failed")
return
end if
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_Token
return
end if
if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
Write-Debug "An OAuth access token is required."
destroy loo_Token
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Token.StringOf("access_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.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=5",loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "Gmail did not return an HTTP response."
destroy loo_Token
destroy loo_Http
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_Token
destroy loo_Http
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_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
return
end if
loo_Ids = create oleobject
li_rc = loo_Ids.ConnectToNewObject("Chilkat.StringTable")
li_Count = loo_Reply.SizeOfArray("messages")
i = 0
do while (i < li_Count)
loo_Reply.I = i
li_Success = loo_Ids.Append(loo_Reply.StringOf("messages[i].id"))
if li_Success <> 1 then
Write-Debug "Could not retain a message ID."
destroy loo_Token
destroy loo_Http
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_Ids.StringAt(i))
loo_Url.Append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date")
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_Token
destroy loo_Http
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_Token
destroy loo_Http
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_Token
destroy loo_Http
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 "Messages inspected: " + string(loo_Ids.Count)
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_Ids
destroy loo_Url