PowerBuilder GMail: Lists the messages in the user's mailbox.

Back to Index

Lists the messages in the user's mailbox.

Documentation: https://developers.google.com/gmail/api/v1/reference/users/messages/list


integer li_rc
oleobject loo_Rest
integer li_Success
oleobject loo_Oauth2
oleobject loo_SbJson
oleobject loo_Json
integer li_ResultSizeEstimate
integer i
integer li_Count_i
string ls_Id
string ls_ThreadId

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat_9_5_0.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//   Provide a previously obtained OAuth2 access token.
loo_Oauth2 = create oleobject
li_rc = loo_Oauth2.ConnectToNewObject("Chilkat_9_5_0.OAuth2")

loo_Oauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
loo_Rest.SetAuthOAuth2(loo_Oauth2)

li_Success = loo_Rest.Connect("www.googleapis.com",443,1,1)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    return
end if

loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

li_Success = loo_Rest.FullRequestNoBodySb("GET","/gmail/v1/users/me/messages",loo_SbJson)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_Oauth2
    destroy loo_SbJson
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "Received error response code: " + string(loo_Rest.ResponseStatusCode)
    Write-Debug "Response body:"
    Write-Debug loo_SbJson.GetAsString()
    destroy loo_Rest
    destroy loo_Oauth2
    destroy loo_SbJson
    return
end if

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_Json.LoadSb(loo_SbJson)

//  The following code parses the JSON response.
//  A sample JSON response is shown below the sample code.

li_ResultSizeEstimate = loo_Json.IntOf("resultSizeEstimate")
i = 0
li_Count_i = loo_Json.SizeOfArray("messages")
do while i < li_Count_i
    loo_Json.I = i
    ls_Id = loo_Json.StringOf("messages[i].id")
    ls_ThreadId = loo_Json.StringOf("messages[i].threadId")
    i = i + 1
loop

Write-Debug "Example Completed."


destroy loo_Rest
destroy loo_Oauth2
destroy loo_SbJson
destroy loo_Json

Sample JSON Response Body

{
  "messages": [
    {
      "id": "15fc237e79da4174",
      "threadId": "15fc237e79da4174"
    },
    {
      "id": "15fc23688c553739",
      "threadId": "15fc23688c553739"
    },
    {
      "id": "15fbd37d3a8f9950",
      "threadId": "15fbd37d3a8f9950"
    },
    {
      "id": "15fb5e49b822ac1d",
      "threadId": "15fb5e49b822ac1d"
    },
    {
      "id": "15fb5e49b7a0739b",
      "threadId": "15fb5e49b7a0739b"
    }
  ],
  "resultSizeEstimate": 5
}