Visual FoxPro GMail: Search for Messages with Subject Containing a Word

Back to Index

Return messages matching the specified query. See https://support.google.com/mail/answer/7190?hl=en for additional information about search operators. This example searches for all emails having the whole word "ADVChina" in the subject.

Documentation: https://support.google.com/mail/answer/7190?hl=en

CURL Command

curl -X GET https://www.googleapis.com/gmail/v1/users/me/messages?q=subject:ADVChina \
    --header "Authorization: Bearer GMAIL_TOKEN"

Visual FoxPro Example

LOCAL loRest
LOCAL lnSuccess
LOCAL lnBTls
LOCAL lnPort
LOCAL lnBAutoReconnect
LOCAL loSbResponseBody
LOCAL lnRespStatusCode
LOCAL loJsonResponse
LOCAL i
LOCAL lnCount_i
LOCAL lnResultSizeEstimate
LOCAL lcId
LOCAL lcThreadId

loRest = CreateObject('Chilkat_9_5_0.Rest')

*  URL: https://www.googleapis.com/gmail/v1/users/me/messages?q=subject:ADVChina
lnBTls = 1
lnPort = 443
lnBAutoReconnect = 1
lnSuccess = loRest.Connect("www.googleapis.com",lnPort,lnBTls,lnBAutoReconnect)
IF (lnSuccess <> 1) THEN
    ? "ConnectFailReason: " + STR(loRest.ConnectFailReason)
    ? loRest.LastErrorText
    RELEASE loRest
    CANCEL
ENDIF

loRest.AddHeader("Authorization","Bearer GMAIL_TOKEN")

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

lnRespStatusCode = loRest.ResponseStatusCode
IF (lnRespStatusCode >= 400) THEN
    ? "Response Status Code = " + STR(lnRespStatusCode)
    ? "Response Header:"
    ? loRest.ResponseHeader
    ? "Response Body:"
    ? loSbResponseBody.GetAsString()
    RELEASE loRest
    RELEASE loSbResponseBody
    CANCEL
ENDIF

loJsonResponse = CreateObject('Chilkat_9_5_0.JsonObject')
loJsonResponse.LoadSb(loSbResponseBody)

*  See the Online Tool for Generating JSON Parse Code

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

RELEASE loRest
RELEASE loSbResponseBody
RELEASE loJsonResponse

Sample JSON Response Body

{
  "messages": [
    {
      "id": "166e50fed0b9b0cb",
      "threadId": "166e50fed0b9b0cb"
    },
    {
      "id": "166c12e5fee013fe",
      "threadId": "166c12e5fee013fe"
    },
    {
      "id": "1669cc9a926bb8c1",
      "threadId": "1669cc9a926bb8c1"
    },
    {
      "id": "16678c485e7f0a0c",
      "threadId": "16678c485e7f0a0c"
    }
  ],
  "resultSizeEstimate": 4
}