Sample code for 30+ languages & platforms
DataFlex

BatchModify - Add a Label to each Message in Search Results

See more GMail REST API Examples

Searchs GMail for messages meeting a criteria and adds a label to each message found.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    String sUserId
    String sQuery
    String sUrl
    Variant vSb
    Handle hoSb
    Handle hoJson
2    Handle hoJson2
    Integer i
    Integer iNumMessages
    String sId
    String sLabelId
    Variant vResp
    Handle hoResp
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComAuthToken Of hoHttp To "GMAIL-ACCESS-TOKEN"

    Move "me" To sUserId
    Get ComSetUrlVar Of hoHttp "userId" sUserId To iSuccess

    Move "subject:questions" To sQuery
    Get ComSetUrlVar Of hoHttp "query" sQuery To iSuccess

    Move "https://www.googleapis.com/gmail/v1/users/{$userId}/messages?q={$query}" To sUrl

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get pvComObject of hoSb to vSb
    Get ComQuickGetSb Of hoHttp sUrl vSb To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get pvComObject of hoSb to vSb
    Get ComLoadSb Of hoJson vSb To iSuccess
    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    Get ComLastStatus Of hoHttp To iTemp1
    If (iTemp1 <> 200) Begin
        Showln "Failed."
        Procedure_Return
    End

    // 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.

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson2
    If (Not(IsComObjectCreated(hoJson2))) Begin
        Send CreateComObject of hoJson2
    End

    Move 0 To i
    Get ComSizeOfArray Of hoJson "messages" To iNumMessages
    While (i < iNumMessages)
        Set ComI Of hoJson To i
        Get ComStringOf Of hoJson "messages[i].id" To sId
        Set ComI Of hoJson2 To i
        Get ComUpdateString Of hoJson2 "ids[i]" sId To iSuccess
        Move (i + 1) To i
    Loop

    // 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"
    Move "Label_43" To sLabelId
    Get ComUpdateString Of hoJson2 "addLabelIds[0]" sLabelId To iSuccess
    Get ComUpdateNewArray Of hoJson2 "removeLabelIds" To iSuccess

    Set ComEmitCompact Of hoJson2 To False
    Get ComEmit Of hoJson2 To sTemp1
    Showln sTemp1

    // Send the batchModify
    Move "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/batchModify" To sUrl
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoJson2 to vJson2
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" sUrl vJson2 "application/json" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln "status = " iTemp1

    // A 204 response status indicate success.
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 <> 204) Begin
        Get ComBodyStr Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_Return
    End

    // The 204 response has an empty response body..

    Showln "BatchModify success!"


End_Procedure