SQL Server Google Drive: List Files

Back to Index

Lists files..

Documentation: https://developers.google.com/drive/v3/reference/files/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

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

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/drive/v3/files', 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 @incompleteSearch int

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @id nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @mimeType nvarchar(4000)

    EXEC sp_OAMethod @json, 'StringOf', @kind OUT, 'kind'
    EXEC sp_OAMethod @json, 'BoolOf', @incompleteSearch OUT, 'incompleteSearch'
    SELECT @i = 0
    EXEC sp_OAMethod @json, 'SizeOfArray', @count_i OUT, 'files'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @json, 'I', STR(@i)
        EXEC sp_OAMethod @json, 'StringOf', @kind OUT, 'files[i].kind'
        EXEC sp_OAMethod @json, 'StringOf', @id OUT, 'files[i].id'
        EXEC sp_OAMethod @json, 'StringOf', @name OUT, 'files[i].name'
        EXEC sp_OAMethod @json, 'StringOf', @mimeType OUT, 'files[i].mimeType'
        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#fileList",
 "incompleteSearch": false,
 "files": [
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolT1E4blNEaHlBVTg",
   "name": "misc",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B84ttUI7E_NyWHBYNUlFRGZSWVE",
   "name": "02112017091435553-LW0826-058444946-4C414E43.xml",
   "mimeType": "application/xml"
  },
  {
   "kind": "drive#file",
   "id": "0B5w0YPzxv8g6SmRDTWUweEJCeVk",
   "name": "Sales in 2004.pdf",
   "mimeType": "application/pdf"
  },
  {
   "kind": "drive#file",
   "id": "0By-hrxCCDzSQUGxJY0hzNUFpNmc",
   "name": "helloWorld.txt",
   "mimeType": "text/plain"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolMjZhSkV0a2tUX3c",
   "name": "hamletC.xml",
   "mimeType": "text/xml"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolVlA1d0tscUtRLXc",
   "name": "chilkat",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolelZ2UGdPZmlJTmc",
   "name": "hamlet2.xml",
   "mimeType": "text/xml"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolaWNqLWJqLUhwb2M",
   "name": "starfish3.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolNlFnbDhWZ3g3UGc",
   "name": "starfish4.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolQlExSlBQT1phZXM",
   "name": "pigs.json",
   "mimeType": "attachment/json"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolVHRPVkxtYWFtZkk",
   "name": "starfish20.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolRGZEV3ZGUTZfNFk",
   "name": "penguins2.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolS2FXSjliMXQxSU0",
   "name": "starfish.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolREhVUzVWRUlFeGM",
   "name": "Пингвины.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolZUhxckMzb0dRMzg",
   "name": "starfish.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolbUF6WS1Gei1oalk",
   "name": "penguins.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYola296ODZUSm5GYU0",
   "name": "123",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolbTE3c3J5RHBUcHM",
   "name": "abc",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolTmhybWJSUGd5Q2c",
   "name": "testHello.txt",
   "mimeType": "text/plain"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolY2tPU1BnYW02T2c",
   "name": "testFolder",
   "mimeType": "application/vnd.google-apps.folder"
  }
 ]
}