Go Requires Chilkat v11.0.0+
Go
List Gmail message IDs across all pages
See more GMail REST API Examples
List message and thread IDs for the authenticated user. Continue with nextPageToken until it is absent. A message ID is a Gmail identifier, distinct from the RFC Message-ID header. Use it in the reading examples. Spam and Trash are excluded by default.
Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Go Downloads
success := false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token := chilkat.NewJsonObject()
success = token.LoadFile("gmail-token.json")
if success != true {
fmt.Println("Could not read gmail-token.json from the private working directory.")
token.DisposeJsonObject()
return
}
if (token.JsonTypeOf("access_token") != 1) || (token.StringOfEquals("access_token","",true) == true) {
fmt.Println("An OAuth access token is required.")
token.DisposeJsonObject()
return
}
http := chilkat.NewHttp()
http.SetRequireSslCertVerify(true)
http.SetAuthToken(token.StringOf("access_token"))
resp := chilkat.NewHttpResponse()
reply := chilkat.NewJsonObject()
total := 0
baseUrl := chilkat.NewStringBuilder()
baseUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100")
url := chilkat.NewStringBuilder()
pageToken := chilkat.NewStringBuilder()
more := true
for (more == true) {
url.SetString(*baseUrl.GetAsString())
if pageToken.Length() > 0 {
url.Append("&pageToken=")
url.Append(*http.UrlEncode(*pageToken.GetAsString()))
}
success = http.HttpNoBody("GET",*url.GetAsString(),resp)
if resp.StatusCode() == 0 {
fmt.Println("Gmail did not return an HTTP response.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
baseUrl.DisposeStringBuilder()
url.DisposeStringBuilder()
pageToken.DisposeStringBuilder()
return
}
if (success != true) || (resp.StatusCode() < 200) || (resp.StatusCode() >= 300) {
fmt.Println("Gmail HTTP ", resp.StatusCode())
success = reply.Load(resp.BodyStr())
if success == true {
fmt.Println(*reply.StringOf("error.message"))
}
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
baseUrl.DisposeStringBuilder()
url.DisposeStringBuilder()
pageToken.DisposeStringBuilder()
return
}
success = reply.Load(resp.BodyStr())
if success != true {
fmt.Println("Expected a Gmail JSON response.")
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
baseUrl.DisposeStringBuilder()
url.DisposeStringBuilder()
pageToken.DisposeStringBuilder()
return
}
// Gmail can omit messages when the search or mailbox is empty.
count := reply.SizeOfArray("messages")
i := 0
for (i < count) {
reply.SetI(i)
fmt.Println(*reply.StringOf("messages[i].id"), " thread ", *reply.StringOf("messages[i].threadId"))
total = total + 1
i = i + 1
}
more = false
if reply.JsonTypeOf("nextPageToken") == 1 {
pageToken.SetString(*reply.StringOf("nextPageToken"))
more = pageToken.Length() > 0
}
}
fmt.Println("Messages listed: ", total)
token.DisposeJsonObject()
http.DisposeHttp()
resp.DisposeHttpResponse()
reply.DisposeJsonObject()
baseUrl.DisposeStringBuilder()
url.DisposeStringBuilder()
pageToken.DisposeStringBuilder()