AutoIt Requires Chilkat v11.0.0+
AutoIt
Apply a Gmail label to search results in batches
See more GMail REST API Examples
Choose a Gmail search query and an existing label name. Resolve the label ID, collect all matching message IDs before changing any labels, then submit at most 1,000 IDs per batchModify call. Empty searches send no updates. For large mailboxes, spool IDs to persistent storage. Completed batches stay applied if a later request fails; adding the same label again is safe.
Authorize the application with https://www.googleapis.com/auth/gmail.modify. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-token.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not read gmail-token.json from the private working directory." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
ConsoleWrite("An OAuth access token is required." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oHttp.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/labels",$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess = True) Then
ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
Exit
EndIf
If ($oReply.JsonTypeOf("labels") <> 4) Then
ConsoleWrite("Expected a list of labels." & @CRLF)
Exit
EndIf
$oLabel = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReply.FindRecord2("labels","name","To review",True,$oLabel)
If ($bSuccess <> True) Then
ConsoleWrite("No label with that exact name. Check the name or create the label first." & @CRLF)
Exit
EndIf
$oLabelId = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oLabel.StringOfSb("id",$oLabelId)
If ($bSuccess <> True) Then
ConsoleWrite("Could not read the label ID." & @CRLF)
Exit
EndIf
$oIds = ObjCreate("Chilkat.StringTable")
$oBaseUrl = ObjCreate("Chilkat.StringBuilder")
$oBaseUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=500")
Local $searchQuery = "has:attachment"
$oBaseUrl.Append("&q=")
$oBaseUrl.Append($oHttp.UrlEncode($searchQuery))
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oPageToken = ObjCreate("Chilkat.StringBuilder")
Local $bMore = True
While ($bMore = True)
$oUrl.SetString($oBaseUrl.GetAsString())
If ($oPageToken.Length > 0) Then
$oUrl.Append("&pageToken=")
$oUrl.Append($oHttp.UrlEncode($oPageToken.GetAsString()))
EndIf
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess = True) Then
ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
Exit
EndIf
; Gmail can omit messages when the search or mailbox is empty.
Local $iCount = $oReply.SizeOfArray("messages")
Local $i = 0
While ($i < $iCount)
$oReply.I = $i
$bSuccess = $oIds.Append($oReply.StringOf("messages[i].id"))
If ($bSuccess <> True) Then
ConsoleWrite("Could not retain a message ID." & @CRLF)
Exit
EndIf
$i = $i + 1
Wend
$bMore = False
If ($oReply.JsonTypeOf("nextPageToken") = 1) Then
$oPageToken.SetString($oReply.StringOf("nextPageToken"))
$bMore = $oPageToken.Length > 0
EndIf
Wend
; Search is complete. Apply the label to each group and track confirmed progress.
$oRequest = ObjCreate("Chilkat.JsonObject")
Local $iDone = 0
While ($iDone < $oIds.Count)
$oRequest.Clear
$oRequest.UpdateString("addLabelIds[0]",$oLabelId.GetAsString())
Local $iN = 0
While (($iN < 1000) And (($iDone + $iN) < $oIds.Count))
$oRequest.I = $iN
$oRequest.UpdateString("ids[i]",$oIds.StringAt($iDone + $iN))
$iN = $iN + 1
Wend
$bSuccess = $oHttp.HttpJson("POST","https://gmail.googleapis.com/gmail/v1/users/me/messages/batchModify",$oRequest,"application/json",$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess = True) Then
ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
EndIf
Exit
EndIf
$iDone = $iDone + $iN
ConsoleWrite("Messages labeled so far: " & $iDone & @CRLF)
Wend
ConsoleWrite("Total messages labeled: " & $iDone & @CRLF)