Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) BatchModify - Add a Label to each Message in Search ResultsSearchs GMail for messages meeting a criteria and adds a label to each message found.
IncludeFile "CkHttpResponse.pb" IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. success.i http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttp::setCkAuthToken(http, "GMAIL-ACCESS-TOKEN") userId.s = "me" CkHttp::ckSetUrlVar(http,"userId",userId) query.s = "subject:questions" CkHttp::ckSetUrlVar(http,"query",query) url.s = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages?q={$query}" sb.i = CkStringBuilder::ckCreate() If sb.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkHttp::ckQuickGetSb(http,url,sb) If success <> 1 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkStringBuilder::ckDispose(sb) ProcedureReturn EndIf json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(json,sb) CkJsonObject::setCkEmitCompact(json, 0) Debug CkJsonObject::ckEmit(json) If CkHttp::ckLastStatus(http) <> 200 Debug "Failed." CkHttp::ckDispose(http) CkStringBuilder::ckDispose(sb) CkJsonObject::ckDispose(json) ProcedureReturn EndIf ; If successful, the received JSON looks like this: ; { ; "messages": [ ; { ; "id": "166f583051d36144", ; "threadId": "166f583051d36144" ; }, ; { ; "id": "166f5815e1f36144", ; "threadId": "166f5815e1f36144" ; }, ; ... ; { ; "id": "166f580639e36144", ; "threadId": "166f580639e36144" ; }, ; { ; "id": "15dbc2e28ec789c6", ; "threadId": "15dbc2e28ec789c6" ; } ; ], ; "nextPageToken": "13434766102274844688", ; "resultSizeEstimate": 103 ; } ; ; Next, we'll be sending an HTTP POST to add the label "questions" to each message in the ; search results. The JSON to be sent for the batchModify is this: ; { ; "ids": [ ; string ; ], ; "addLabelIds": [ ; string ; ], ; "removeLabelIds": [ ; string ; ] ; } ; We'll omit "removeLabelIds" because we're not removing any labels. ; We are parsing the JSON search results, and at the same time building the batchModify JSON. json2.i = CkJsonObject::ckCreate() If json2.i = 0 Debug "Failed to create object." ProcedureReturn EndIf i.i = 0 numMessages.i = CkJsonObject::ckSizeOfArray(json,"messages") While (i < numMessages) CkJsonObject::setCkI(json, i) id.s = CkJsonObject::ckStringOf(json,"messages[i].id") CkJsonObject::setCkI(json2, i) CkJsonObject::ckUpdateString(json2,"ids[i]",id) i = i + 1 Wend ; We need the id of the label (not the name). ; I know the name of the label is "questions", but I need to know the id. ; See this example: Get Label Id by Name ; The id of my label named "questions" is "Label_43" labelId.s = "Label_43" CkJsonObject::ckUpdateString(json2,"addLabelIds[0]",labelId) CkJsonObject::ckUpdateNewArray(json2,"removeLabelIds") CkJsonObject::setCkEmitCompact(json2, 0) Debug CkJsonObject::ckEmit(json2) ; Send the batchModify url = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/batchModify" resp.i = CkHttp::ckPostJson3(http,url,"application/json",json2) If CkHttp::ckLastMethodSuccess(http) <> 1 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkStringBuilder::ckDispose(sb) CkJsonObject::ckDispose(json) CkJsonObject::ckDispose(json2) ProcedureReturn EndIf Debug "status = " + Str(CkHttpResponse::ckStatusCode(resp)) ; A 204 response status indicate success. If CkHttpResponse::ckStatusCode(resp) <> 204 Debug CkHttpResponse::ckBodyStr(resp) Debug "Failed." CkHttpResponse::ckDispose(resp) CkHttp::ckDispose(http) CkStringBuilder::ckDispose(sb) CkJsonObject::ckDispose(json) CkJsonObject::ckDispose(json2) ProcedureReturn EndIf ; The 204 response has an empty response body.. CkHttpResponse::ckDispose(resp) Debug "BatchModify success!" CkHttp::ckDispose(http) CkStringBuilder::ckDispose(sb) CkJsonObject::ckDispose(json) CkJsonObject::ckDispose(json2) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.