Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) BatchModify - Add a Label to each Message in Search Results

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

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
    Boolean bTemp1

    // 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 <> True) 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 pvComObject of hoJson2 to vJson2
    Get ComPostJson3 Of hoHttp sUrl "application/json" vJson2 To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) 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."
        Send Destroy of hoResp
        Procedure_Return
    End

    // The 204 response has an empty response body..
    Send Destroy of hoResp

    Showln "BatchModify success!"


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.