SQL Server Google Drive: List Changes to a TeamDrive

Back to Index

Download a list of changes to a teamdrive. This example lists the changes for the team drive having id = "0AEd3EhGff2SaUk9PVA". The "pageToken" is obtained from a previous call to /drive/v3/changes/startPageToken or from the "nextPageToken" member of the previous call to list changes.

Documentation: https://developers.google.com/drive/v3/reference/changes/list


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, 'AddQueryParam', @success OUT, 'teamDriveId', '0AEd3EhGff2SaUk9PVA'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'pageToken', '13'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'includeTeamDriveItems', 'true'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'supportsTeamDrives', 'true'

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

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/drive/v3/changes', 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 @kind nvarchar(4000)

    DECLARE @newStartPageToken nvarchar(4000)

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @type nvarchar(4000)

    DECLARE @time nvarchar(4000)

    DECLARE @removed int

    DECLARE @fileId nvarchar(4000)

    DECLARE @fileKind nvarchar(4000)

    DECLARE @fileName nvarchar(4000)

    DECLARE @fileMimeType nvarchar(4000)

    DECLARE @fileTeamDriveId nvarchar(4000)

    EXEC sp_OAMethod @json, 'StringOf', @kind OUT, 'kind'
    EXEC sp_OAMethod @json, 'StringOf', @newStartPageToken OUT, 'newStartPageToken'
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'changes'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @json, 'I', STR(@i)
        EXEC sp_OAMethod @json, 'StringOf', @kind OUT, 'changes[i].kind'
        EXEC sp_OAMethod @json, 'StringOf', @type OUT, 'changes[i].type'
        EXEC sp_OAMethod @json, 'StringOf', @time OUT, 'changes[i].time'
        EXEC sp_OAMethod @json, 'BoolOf', @removed OUT, 'changes[i].removed'
        EXEC sp_OAMethod @json, 'StringOf', @fileId OUT, 'changes[i].fileId'
        EXEC sp_OAMethod @json, 'StringOf', @fileKind OUT, 'changes[i].file.kind'
        EXEC sp_OAMethod @json, 'StringOf', @fileId OUT, 'changes[i].file.id'
        EXEC sp_OAMethod @json, 'StringOf', @fileName OUT, 'changes[i].file.name'
        EXEC sp_OAMethod @json, 'StringOf', @fileMimeType OUT, 'changes[i].file.mimeType'
        EXEC sp_OAMethod @json, 'StringOf', @fileTeamDriveId OUT, 'changes[i].file.teamDriveId'
        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

{
  "kind": "drive#changeList",
  "newStartPageToken": "16",
  "changes": [
    {
      "kind": "drive#change",
      "type": "file",
      "time": "2017-11-13T17:04:47.470Z",
      "removed": false,
      "fileId": "1lT4TbeSSMtxgB2MmwE7-5i7vQaxhr6Ze",
      "file": {
        "kind": "drive#file",
        "id": "1lT4TbeSSMtxgB2MmwE7-5i7vQaxhr6Ze",
        "name": "starfish2.jpg",
        "mimeType": "image/jpeg",
        "teamDriveId": "0AEd3EhGff2SaUk9PVA"
      }
    },
    {
      "kind": "drive#change",
      "type": "file",
      "time": "2017-11-13T17:05:01.095Z",
      "removed": false,
      "fileId": "16MsxSRSz6ISRx1D_PXyA5Sj6aNFSEd4e",
      "file": {
        "kind": "drive#file",
        "id": "16MsxSRSz6ISRx1D_PXyA5Sj6aNFSEd4e",
        "name": "seahorse.jpg",
        "mimeType": "image/jpeg",
        "teamDriveId": "0AEd3EhGff2SaUk9PVA"
      }
    }
  ]
}