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

Back to Index

Gets a specific message using format=metadata.

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', 'metadata'

    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 @payloadMimeType nvarchar(4000)

    DECLARE @sizeEstimate int

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @strVal nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @value nvarchar(4000)

    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', @payloadMimeType OUT, 'payload.mimeType'
    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


    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": {
    "mimeType": "multipart/mixed",
    "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\""
      }
    ]
  },
  "sizeEstimate": 11716
}