Visual FoxPro 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


LOCAL loRest
LOCAL lnSuccess
LOCAL loOauth2
LOCAL loSbJson
LOCAL loJson
LOCAL lnResultSizeEstimate
LOCAL i
LOCAL lnCount_i
LOCAL lcId
LOCAL lcThreadId

loRest = CreateObject('Chilkat_9_5_0.Rest')

*   Provide a previously obtained OAuth2 access token.
loOauth2 = CreateObject('Chilkat_9_5_0.OAuth2')
loOauth2.AccessToken = "OAUTH2_ACCESS_TOKEN"
loRest.SetAuthOAuth2(loOauth2)

lnSuccess = loRest.Connect("www.googleapis.com",443,1,1)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loRest
    RELEASE loOauth2
    CANCEL
ENDIF

loSbJson = CreateObject('Chilkat_9_5_0.StringBuilder')
lnSuccess = loRest.FullRequestNoBodySb("GET","/gmail/v1/users/me/messages",loSbJson)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loRest
    RELEASE loOauth2
    RELEASE loSbJson
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "Received error response code: " + STR(loRest.ResponseStatusCode)
    ? "Response body:"
    ? loSbJson.GetAsString()
    RELEASE loRest
    RELEASE loOauth2
    RELEASE loSbJson
    CANCEL
ENDIF

loJson = CreateObject('Chilkat_9_5_0.JsonObject')
loJson.LoadSb(loSbJson)

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

lnResultSizeEstimate = loJson.IntOf("resultSizeEstimate")
i = 0
lnCount_i = loJson.SizeOfArray("messages")
DO WHILE i < lnCount_i
    loJson.I = i
    lcId = loJson.StringOf("messages[i].id")
    lcThreadId = loJson.StringOf("messages[i].threadId")
    i = i + 1
ENDDO

? "Example Completed."

RELEASE loRest
RELEASE loOauth2
RELEASE loSbJson
RELEASE loJson

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
}