SQL Server GMail: List all GMail Labels

Back to Index

Lists all labels in the user's mailbox.

Documentation: https://developers.google.com/gmail/api/v1/reference/users/labels/list

CURL Command

curl -X GET https://www.googleapis.com/gmail/v1/users/me/labels \
    --header "Authorization: Bearer GMAIL_TOKEN"

SQL Server Example

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

    --  URL: https://www.googleapis.com/gmail/v1/users/me/labels
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'www.googleapis.com', @port, @bTls, @bAutoReconnect
    IF @success <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', 'Bearer GMAIL_TOKEN'

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

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/gmail/v1/users/me/labels', @sbResponseBody
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END
    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @respStatusCode OUT
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Status Code = ' + @respStatusCode

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Response Body:'
        EXEC sp_OAMethod @sbResponseBody, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

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

    EXEC sp_OAMethod @jsonResponse, 'LoadSb', @success OUT, @sbResponseBody

    --  See the Online Tool for Generating JSON Parse Code
    DECLARE @i int

    DECLARE @count_i int

    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'labels'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', @i
        DECLARE @id nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'labels[i].id'
        DECLARE @name nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @name OUT, 'labels[i].name'
        DECLARE @messageListVisibility nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @messageListVisibility OUT, 'labels[i].messageListVisibility'
        DECLARE @labelListVisibility nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @labelListVisibility OUT, 'labels[i].labelListVisibility'
        DECLARE @type nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @type OUT, 'labels[i].type'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jsonResponse


END
GO

Sample JSON Response Body

{
  "labels": [
    {
      "id": "Label_5",
      "name": "QA",
      "messageListVisibility": "show",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "Label_21",
      "name": "[Gmail]/testFolder",
      "type": "user"
    },
    {
      "id": "CATEGORY_PERSONAL",
      "name": "CATEGORY_PERSONAL",
      "type": "system"
    },
    {
      "id": "Label_8",
      "name": "old",
      "type": "user"
    },
    {
      "id": "CATEGORY_SOCIAL",
      "name": "CATEGORY_SOCIAL",
      "type": "system"
    },
    {
      "id": "Label_7",
      "name": "labél with space",
      "type": "user"
    },
    {
      "id": "Label_41",
      "name": "QA/qa_fetchSingleAsMime",
      "type": "user"
    },
    {
      "id": "Label_4",
      "name": "[Imap]/Trash",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "Label_1",
      "name": "\"test\"",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "Label_6",
      "name": "QA/Entwürfe",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "CATEGORY_FORUMS",
      "name": "CATEGORY_FORUMS",
      "type": "system"
    },
    {
      "id": "Label_42",
      "name": "[Gmail]/フルタイムの共有ソリューションはjapan",
      "type": "user"
    },
    {
      "id": "Label_3",
      "name": "[Gmail]/ö",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "user"
    },
    {
      "id": "IMPORTANT",
      "name": "IMPORTANT",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "Label_2",
      "name": "[Gmail]/X&Y",
      "type": "user"
    },
    {
      "id": "CATEGORY_UPDATES",
      "name": "CATEGORY_UPDATES",
      "type": "system"
    },
    {
      "id": "CHAT",
      "name": "CHAT",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "SENT",
      "name": "SENT",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "INBOX",
      "name": "INBOX",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "TRASH",
      "name": "TRASH",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelHide",
      "type": "system"
    },
    {
      "id": "CATEGORY_PROMOTIONS",
      "name": "CATEGORY_PROMOTIONS",
      "type": "system"
    },
    {
      "id": "DRAFT",
      "name": "DRAFT",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "SPAM",
      "name": "SPAM",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelHide",
      "type": "system"
    },
    {
      "id": "STARRED",
      "name": "STARRED",
      "messageListVisibility": "hide",
      "labelListVisibility": "labelShow",
      "type": "system"
    },
    {
      "id": "UNREAD",
      "name": "UNREAD",
      "type": "system"
    }
  ]
}