Get number of emails in mailbox for Gmail with Chilkat

I can connect to Gmail with OAuth. The code below to get the number of emails in a mailbox works fine for the Inbox. But for other mailboxes I only get an empty result.

Dim http As New Chilkat.Http
http.AuthToken = getTokenValueFromToken
http.Accept = "application/json"

Dim mailboxId As String = "Label_5600941813016058084" 'GetLabelFromMailbox(hasMailboxPath)
if mailboxId = "" then Break
Dim apiEndpoint As String = "https://www.googleapis.com/gmail/v1/users/me/messages?q=label:" + mailboxId

Dim theHttpResponse As Chilkat.HttpResponse
theHttpResponse = http.QuickRequest("GET", apiEndpoint)
If not theHttpResponse.LastMethodSuccess Then
  theError = theHttpResponse.LastErrorText
  Return 0
End If

If theHttpResponse.StatusCode <> 200 Then
  theError = "Http for getting the mailbox count failed. Error code: " + http.LastStatus.ToString
  Return 0
End If

Dim jsonResponse As New Chilkat.JsonObject
Dim success As Boolean = jsonResponse.Load(theHttpResponse.BodyStr)

//this is the count of emails
Dim resultSizeEstimate As Integer = jsonResponse.IntOf("resultSizeEstimate")

I have code to get all mailboxes with their labels so that I can get the label from a mailbox with GetLabelFromMailbox. Here I used an existing label for testing.

What am I doing wrong?

The API explorer at https://developers.google.com/gmail/api/reference/rest/v1/users.messages/list gives the correct result.

I think you’ll need Chilkat support.

Chilkat Technical Help

1 Like

Yeah, I’ll contact their support.

1 Like