SQL Server GMail: Get a specific message (format="full")

Back to Index

Gets a specific message using format=full.

Documentation: https://developers.google.com/gmail/api/v1/reference/users/messages/get


CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --   Provide a previously obtained OAuth2 access token.
    DECLARE @oauth2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.OAuth2', @oauth2 OUT

    EXEC sp_OASetProperty @oauth2, 'AccessToken', 'OAUTH2_ACCESS_TOKEN'
    EXEC sp_OAMethod @rest, 'SetAuthOAuth2', @success OUT, STR(@oauth2)

    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'www.googleapis.com', 443, 1, 1
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddPathParam', @success OUT, 'messageId', '15fc23688c553739'

    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'format', 'full'

    DECLARE @sbJson int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbJson OUT

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/gmail/v1/users/me/messages/messageId', STR(@sbJson)
    IF STR(@success) <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        EXEC @hr = sp_OADestroy @sbJson
        RETURN
      END

    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        PRINT 'Received error response code: ' + @iTmp0

        PRINT 'Response body:'
        EXEC sp_OAMethod @sbJson, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth2
        EXEC @hr = sp_OADestroy @sbJson
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'LoadSb', @success OUT, STR(@sbJson)

    --  The following code parses the JSON response.
    --  A sample JSON response is shown below the sample code.
    DECLARE @id nvarchar(4000)

    DECLARE @threadId nvarchar(4000)

    DECLARE @snippet nvarchar(4000)

    DECLARE @historyId nvarchar(4000)

    DECLARE @internalDate nvarchar(4000)

    DECLARE @payloadPartId nvarchar(4000)

    DECLARE @payloadMimeType nvarchar(4000)

    DECLARE @payloadFilename nvarchar(4000)

    DECLARE @payloadBodySize int

    DECLARE @sizeEstimate int

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @strVal nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @value nvarchar(4000)

    DECLARE @partId nvarchar(4000)

    DECLARE @mimeType nvarchar(4000)

    DECLARE @filename nvarchar(4000)

    DECLARE @bodySize int

    DECLARE @bodyAttachmentId nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @k int

    DECLARE @count_k int

    DECLARE @bodyData nvarchar(4000)

    DECLARE @i1 int

    DECLARE @count_i1 int

    DECLARE @json1 int

    EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'id'
    EXEC sp_OAMethod @json, 'StringOf', @threadId OUT, 'threadId'
    EXEC sp_OAMethod @json, 'StringOf', @snippet OUT, 'snippet'
    EXEC sp_OAMethod @json, 'StringOf', @historyId OUT, 'historyId'
    EXEC sp_OAMethod @json, 'StringOf', @internalDate OUT, 'internalDate'
    EXEC sp_OAMethod @json, 'StringOf', @payloadPartId OUT, 'payload.partId'
    EXEC sp_OAMethod @json, 'StringOf', @payloadMimeType OUT, 'payload.mimeType'
    EXEC sp_OAMethod @json, 'StringOf', @payloadFilename OUT, 'payload.filename'
    EXEC sp_OAMethod @json, 'IntOf', @payloadBodySize OUT, 'payload.body.size'
    EXEC sp_OAMethod @json, 'IntOf', @sizeEstimate OUT, 'sizeEstimate'
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'labelIds'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @json, 'I', STR(@i)
        EXEC sp_OAMethod @json, 'StringOf', @strVal OUT, 'labelIds[i]'
        SELECT @i = STR(@i) + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'payload.headers'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @json, 'I', STR(@i)
        EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'payload.headers[i].name'
        EXEC sp_OAMethod @json, 'StringOf', @value OUT, 'payload.headers[i].value'
        SELECT @i = STR(@i) + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'payload.parts'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @json, 'I', STR(@i)
        EXEC sp_OAMethod @json, 'StringOf', @partId OUT, 'payload.parts[i].partId'
        EXEC sp_OAMethod @json, 'StringOf', @mimeType OUT, 'payload.parts[i].mimeType'
        EXEC sp_OAMethod @json, 'StringOf', @filename OUT, 'payload.parts[i].filename'
        EXEC sp_OAMethod @json, 'IntOf', @bodySize OUT, 'payload.parts[i].body.size'
        EXEC sp_OAMethod @json, 'StringOf', @bodyAttachmentId OUT, 'payload.parts[i].body.attachmentId'
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'payload.parts[i].headers'
        WHILE STR(@j) < STR(@count_j)
          BEGIN
            EXEC sp_OASetProperty @json, 'J', STR(@j)
            EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'payload.parts[i].headers[j].name'
            EXEC sp_OAMethod @json, 'StringOf', @value OUT, 'payload.parts[i].headers[j].value'
            SELECT @j = STR(@j) + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @json, 'SizeOfArray', @count_j OUT, 'payload.parts[i].parts'
        WHILE STR(@j) < STR(@count_j)
          BEGIN
            EXEC sp_OASetProperty @json, 'J', STR(@j)
            EXEC sp_OAMethod @json, 'StringOf', @partId OUT, 'payload.parts[i].parts[j].partId'
            EXEC sp_OAMethod @json, 'StringOf', @mimeType OUT, 'payload.parts[i].parts[j].mimeType'
            EXEC sp_OAMethod @json, 'StringOf', @filename OUT, 'payload.parts[i].parts[j].filename'
            EXEC sp_OAMethod @json, 'IntOf', @bodySize OUT, 'payload.parts[i].parts[j].body.size'
            EXEC sp_OAMethod @json, 'StringOf', @bodyAttachmentId OUT, 'payload.parts[i].parts[j].body.attachmentId'
            SELECT @k = 0
            EXEC sp_OAMethod @json, 'SizeOfArray', @count_k OUT, 'payload.parts[i].parts[j].headers'
            WHILE STR(@k) < STR(@count_k)
              BEGIN
                EXEC sp_OASetProperty @json, 'K', STR(@k)
                EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'payload.parts[i].parts[j].headers[k].name'
                EXEC sp_OAMethod @json, 'StringOf', @value OUT, 'payload.parts[i].parts[j].headers[k].value'
                SELECT @k = STR(@k) + 1
              END
            SELECT @k = 0
            EXEC sp_OAMethod @json, 'SizeOfArray', @count_k OUT, 'payload.parts[i].parts[j].parts'
            WHILE STR(@k) < STR(@count_k)
              BEGIN
                EXEC sp_OASetProperty @json, 'K', STR(@k)
                EXEC sp_OAMethod @json, 'StringOf', @partId OUT, 'payload.parts[i].parts[j].parts[k].partId'
                EXEC sp_OAMethod @json, 'StringOf', @mimeType OUT, 'payload.parts[i].parts[j].parts[k].mimeType'
                EXEC sp_OAMethod @json, 'StringOf', @filename OUT, 'payload.parts[i].parts[j].parts[k].filename'
                EXEC sp_OAMethod @json, 'IntOf', @bodySize OUT, 'payload.parts[i].parts[j].parts[k].body.size'
                EXEC sp_OAMethod @json, 'StringOf', @bodyData OUT, 'payload.parts[i].parts[j].parts[k].body.data'

                EXEC sp_OAMethod @json, 'ObjectOf', @json1 OUT, 'payload.parts[i].parts[j].parts[k]'
                SELECT @i1 = 0
                EXEC sp_OAMethod @json1, 'SizeOfArray', @count_i1 OUT, 'headers'
                WHILE STR(@i1) < STR(@count_i1)
                  BEGIN
                    EXEC sp_OASetProperty @json1, 'I', STR(@i1)
                    EXEC sp_OAMethod @json1, 'StringOf', @name OUT, 'headers[i].name'
                    EXEC sp_OAMethod @json1, 'StringOf', @value OUT, 'headers[i].value'
                    SELECT @i1 = STR(@i1) + 1
                  END
                EXEC @hr = sp_OADestroy @json1

                SELECT @k = STR(@k) + 1
              END
            SELECT @j = STR(@j) + 1
          END
        SELECT @i = STR(@i) + 1
      END


    PRINT 'Example Completed.'

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @oauth2
    EXEC @hr = sp_OADestroy @sbJson
    EXEC @hr = sp_OADestroy @json


END
GO

Sample JSON Response Body

{
  "id": "15fc23688c553739",
  "threadId": "15fc23688c553739",
  "labelIds": [
    "IMPORTANT",
    "SENT",
    "INBOX"
  ],
  "snippet": "This is a test. \u200b",
  "historyId": "1438",
  "internalDate": "1510791874000",
  "payload": {
    "partId": "",
    "mimeType": "multipart/mixed",
    "filename": "",
    "headers": [
      {
        "name": "MIME-Version",
        "value": "1.0"
      },
      {
        "name": "Received",
        "value": "by 10.25.23.105 with HTTP; Wed, 15 Nov 2017 16:24:34 -0800 (PST)"
      },
      {
        "name": "X-Originating-IP",
        "value": "[2601:249:e00:9093:e894:b27f:e5d6:d01d]"
      },
      {
        "name": "Date",
        "value": "Wed, 15 Nov 2017 18:24:34 -0600"
      },
      {
        "name": "Delivered-To",
        "value": "matt@chilkat.io"
      },
      {
        "name": "Message-ID",
        "value": "\u003cCAPhjyBM1qqXS6NVB65W8Zt5-McF03qWSRDjW5am66wTMuard=w@mail.gmail.com\u003e"
      },
      {
        "name": "Subject",
        "value": "Test HTML email with image and attachment."
      },
      {
        "name": "From",
        "value": "Matt Fausey \u003cmatt@chilkat.io\u003e"
      },
      {
        "name": "To",
        "value": "Matt Fausey \u003cmatt@chilkat.io\u003e"
      },
      {
        "name": "Content-Type",
        "value": "multipart/mixed; boundary=\"001a11401a6c097887055e0ea572\""
      }
    ],
    "body": {
      "size": 0
    },
    "parts": [
      {
        "partId": "0",
        "mimeType": "multipart/related",
        "filename": "",
        "headers": [
          {
            "name": "Content-Type",
            "value": "multipart/related; boundary=\"001a11401a6c097885055e0ea571\""
          }
        ],
        "body": {
          "size": 0
        },
        "parts": [
          {
            "partId": "0.0",
            "mimeType": "multipart/alternative",
            "filename": "",
            "headers": [
              {
                "name": "Content-Type",
                "value": "multipart/alternative; boundary=\"001a11401a6c097882055e0ea570\""
              }
            ],
            "body": {
              "size": 0
            },
            "parts": [
              {
                "partId": "0.0.0",
                "mimeType": "text/plain",
                "filename": "",
                "headers": [
                  {
                    "name": "Content-Type",
                    "value": "text/plain; charset=\"UTF-8\""
                  },
                  {
                    "name": "Content-Transfer-Encoding",
                    "value": "quoted-printable"
                  }
                ],
                "body": {
                  "size": 22,
                  "data": "VGhpcyBpcyBhIHRlc3QuDQrigIsNCg=="
                }
              },
              {
                "partId": "0.0.1",
                "mimeType": "text/html",
                "filename": "",
                "headers": [
                  {
                    "name": "Content-Type",
                    "value": "text/html; charset=\"UTF-8\""
                  },
                  {
                    "name": "Content-Transfer-Encoding",
                    "value": "quoted-printable"
                  }
                ],
                "body": {
                  "size": 115,
                  "data": "PGRpdiBkaXI9Imx0ciI-VGhpcyBpcyBhIHRlc3QuPGltZyB3aWR0aD0iMTIwIiBoZWlnaHQ9IjEyMCIgc3JjPSJjaWQ6aWlfamExcWY5MGwwXzE1ZmMyMzUzOWJkMDA3MjciPjxicj7igIs8L2Rpdj4NCg=="
                }
              }
            ]
          },
          {
            "partId": "0.1",
            "mimeType": "image/jpeg",
            "filename": "starfish.jpg",
            "headers": [
              {
                "name": "Content-Type",
                "value": "image/jpeg; name=\"starfish.jpg\""
              },
              {
                "name": "Content-Disposition",
                "value": "inline; filename=\"starfish.jpg\""
              },
              {
                "name": "Content-Transfer-Encoding",
                "value": "base64"
              },
              {
                "name": "Content-ID",
                "value": "\u003cii_ja1qf90l0_15fc23539bd00727\u003e"
              },
              {
                "name": "X-Attachment-Id",
                "value": "ii_ja1qf90l0_15fc23539bd00727"
              },
              {
                "name": "MIME-Version",
                "value": "1.0"
              },
              {
                "name": "Date",
                "value": "Wed, 15 Nov 2017 16:23:08 -0800"
              },
              {
                "name": "Message-ID",
                "value": "\u003cCAPhjyBMicJ9k-EXY5Efcvp8o_z_J3x8_G=mnC_znLXnyBogemQ@mail.gmail.com\u003e"
              },
              {
                "name": "Subject",
                "value": "Attachment"
              },
              {
                "name": "From",
                "value": "Matt Fausey \u003cmatt@chilkat.io\u003e"
              }
            ],
            "body": {
              "attachmentId": "ANGjdJ_ueuzzM9bpQ8l_tD27oDfn-smCe77viIAF1wy8ZeI09tK5vAuSS6V9Bahow4ikqaGleXqdQBeq8xhFkJ7WN-KKBSKvOfT2yD-XMlA1FC8AOKS2A8tBi5dDDbFrbnns33rfqEIRFAISslhdE481uB6sYfVoYjpuDThtW6Y96YA0Aso0P9txbKJwcwOeD0RxDav_vPzDjHuRuOYsGxbVTxesMB2eiMMw3i1y-WAu-pyWHqF0xMFehZmd7d-krKKdvI4UEgtzD9siXnnaH6MG55ykjBsjFRsmjKc-4K5fHeYHOf586Pkej5K238A",
              "size": 6229
            }
          }
        ]
      },
      {
        "partId": "1",
        "mimeType": "application/pdf",
        "filename": "helloWorld.pdf",
        "headers": [
          {
            "name": "Content-Type",
            "value": "application/pdf; name=\"helloWorld.pdf\""
          },
          {
            "name": "Content-Disposition",
            "value": "attachment; filename=\"helloWorld.pdf\""
          },
          {
            "name": "Content-Transfer-Encoding",
            "value": "base64"
          },
          {
            "name": "X-Attachment-Id",
            "value": "f_ja1qgya31"
          }
        ],
        "body": {
          "attachmentId": "ANGjdJ934ScA-kvdsvBvOjO9z-RYmRLyO8PvqOCzwkk-smeLVpKqeoxXNIn9FL_RbzA6qmTWKQTJehrBVmX4NqYtFsivmwRfzIWj-A-SqvJATRhjEGOntUeBS0fPZ9xy_enmH5Qgi0iSdBmwAn1CBnNghEKdMjvxTMjLxqn2mSO4gvxIh8ahJZDMN1p8KMYO98dM2XfyNndltnK-sd_DizNR9BQu10w5WSTLeFfOh61SxGKeq4Dt3W5USQlx7dEG7dRiBDvjFM6wo2HVeSnGEVphOwx80t1MQdhRN-YOHCAahIqy7Z0-BkPIIjTeUtl_pST8oMi0IgJoo9x1oomY3JZp2OxWuNyOVWDGVsDavGHc6wRhwK5pJrDceCWxNdPEwExeYA84WOAFXE2D6I3m",
          "size": 934
        }
      }
    ]
  },
  "sizeEstimate": 11716
}