From 3336de2c401aed44e4d8f040a246608af871ebb8 Mon Sep 17 00:00:00 2001 From: Justin Szaro Date: Wed, 20 Dec 2023 18:42:07 -0500 Subject: [PATCH 1/8] feat: include a create permissions sample in the drive folder --- samples/drive/create-permission.js | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 samples/drive/create-permission.js diff --git a/samples/drive/create-permission.js b/samples/drive/create-permission.js new file mode 100644 index 0000000000..c52eac7d87 --- /dev/null +++ b/samples/drive/create-permission.js @@ -0,0 +1,53 @@ +// Copyright 2016 Google LLC +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {google} = require('googleapis'); +const fs = require('fs'); +const os = require('os'); +const uuid = require('uuid'); +const path = require('path'); +const {authenticate} = require('@google-cloud/local-auth'); + +const drive = google.drive('v3'); + +async function runSample(fileId) { + // Obtain user credentials to use for the request + const auth = await authenticate({ + keyfilePath: path.join(__dirname, '../oauth2.keys.json'), + scopes: [ + 'https://www.googleapis.com/auth/drive', + 'https://www.googleapis.com/auth/drive.file', + ], + }); + google.options({auth}); + + const res = await drive.permissions.create( + { + fileId: fileId, + requestBody: { + role: 'reader', + type: 'anyone', + }, + } + ); + console.log(res.data); + return res.data; +} + +// if invoked directly (not tests), authenticate and run the samples +if (module === require.main) { + const fileId = process.argv[2]; + runSample(fileId).catch(console.error); +} \ No newline at end of file From 997c4aa39fa7dfa73bfb25439f6bfb0b9a7b0c54 Mon Sep 17 00:00:00 2001 From: Justin Szaro Date: Wed, 20 Dec 2023 19:30:01 -0500 Subject: [PATCH 2/8] fix: remove unused requires and check module --- samples/drive/create-permission.js | 49 ++++++++++++++---------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/samples/drive/create-permission.js b/samples/drive/create-permission.js index c52eac7d87..6a05cd413f 100644 --- a/samples/drive/create-permission.js +++ b/samples/drive/create-permission.js @@ -14,40 +14,35 @@ 'use strict'; const {google} = require('googleapis'); -const fs = require('fs'); -const os = require('os'); -const uuid = require('uuid'); const path = require('path'); const {authenticate} = require('@google-cloud/local-auth'); const drive = google.drive('v3'); async function runSample(fileId) { - // Obtain user credentials to use for the request - const auth = await authenticate({ - keyfilePath: path.join(__dirname, '../oauth2.keys.json'), - scopes: [ - 'https://www.googleapis.com/auth/drive', - 'https://www.googleapis.com/auth/drive.file', - ], - }); - google.options({auth}); - - const res = await drive.permissions.create( - { - fileId: fileId, - requestBody: { - role: 'reader', - type: 'anyone', - }, - } - ); - console.log(res.data); - return res.data; + // Obtain user credentials to use for the request + const auth = await authenticate({ + keyfilePath: path.join(__dirname, '../oauth2.keys.json'), + scopes: [ + 'https://www.googleapis.com/auth/drive', + 'https://www.googleapis.com/auth/drive.file', + ], + }); + google.options({auth}); + + const res = await drive.permissions.create({ + fileId: fileId, + requestBody: { + role: 'reader', + type: 'anyone', + }, + }); + console.log(res.data); + return res.data; } // if invoked directly (not tests), authenticate and run the samples if (module === require.main) { - const fileId = process.argv[2]; - runSample(fileId).catch(console.error); -} \ No newline at end of file + const fileId = process.argv[2]; + runSample(fileId).catch(console.error); +} From aed3ec633a6a8bf07e91df1ca8432e30f69114df Mon Sep 17 00:00:00 2001 From: Justin Szaro Date: Wed, 20 Dec 2023 19:30:11 -0500 Subject: [PATCH 3/8] test: add a test for create permission --- samples/test/test.samples.drive.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/samples/test/test.samples.drive.js b/samples/test/test.samples.drive.js index 48c7bdc3e3..bc7e2ac3aa 100644 --- a/samples/test/test.samples.drive.js +++ b/samples/test/test.samples.drive.js @@ -29,6 +29,7 @@ const samples = { export: {path: '../drive/export'}, list: {path: '../drive/list'}, upload: {path: '../drive/upload'}, + createPermission: {path: '../drive/create-permission'}, }; for (const sample of Object.values(samples)) { @@ -87,4 +88,14 @@ describe('Drive samples', () => { assert(data); scope.done(); }); + + it('should create a permission', async () => { + const fileId = '0B7l5uajXUzaFa0x6cjJfZEkzZVE'; + const scope = nock(baseUrl) + .post(`/drive/v3/files/${fileId}/permissions`) + .reply(200, {}); + const data = await samples.createPermission.runSample(fileId); + assert(data); + scope.done(); + }); }); From b2a6275f5a37f6eb824c6aac41d956451770cd99 Mon Sep 17 00:00:00 2001 From: Justin Szaro Date: Sat, 30 Dec 2023 16:10:33 -0500 Subject: [PATCH 4/8] docs: add create permission example to drive samples --- samples/drive/README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/samples/drive/README.md b/samples/drive/README.md index 60953c1a3b..3fc10e3740 100644 --- a/samples/drive/README.md +++ b/samples/drive/README.md @@ -1,6 +1,6 @@ # Drive v3 API Samples -These samples allow you to list, download, and upload files from Google Drive. +These samples allow you to list, download, upload, and change the permissions of files from Google Drive. ## Running the samples @@ -67,3 +67,17 @@ Example: ``` node export.js 0B_Klegupc5gUcXhFZjZVUV9NeE0 ./file.pdf ``` + +__Run the `create-permission.js` sample:__ + +``` +node create-permission.js +``` + +where `` is the id of any file in Google Drive. + +Example: + +``` +node create-permission.js 0B_Klegupc5gUcXhFZjZVUV9NeE0 +``` From 12352d73f6e0bc396ab034131b50eac68028e63f Mon Sep 17 00:00:00 2001 From: Justin Szaro Date: Sat, 30 Dec 2023 16:43:52 -0500 Subject: [PATCH 5/8] refactor: change file id to match the download doc example --- samples/test/test.samples.drive.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/test/test.samples.drive.js b/samples/test/test.samples.drive.js index bc7e2ac3aa..73edb6276e 100644 --- a/samples/test/test.samples.drive.js +++ b/samples/test/test.samples.drive.js @@ -90,7 +90,7 @@ describe('Drive samples', () => { }); it('should create a permission', async () => { - const fileId = '0B7l5uajXUzaFa0x6cjJfZEkzZVE'; + const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA'; const scope = nock(baseUrl) .post(`/drive/v3/files/${fileId}/permissions`) .reply(200, {}); From a9b3a791d6f6997a0128ff2c214630dc100ade52 Mon Sep 17 00:00:00 2001 From: Justin Szaro Date: Sat, 30 Dec 2023 16:56:17 -0500 Subject: [PATCH 6/8] refactor: add missing module.exports statement --- samples/drive/create-permission.js | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/drive/create-permission.js b/samples/drive/create-permission.js index 6a05cd413f..5ec42a775b 100644 --- a/samples/drive/create-permission.js +++ b/samples/drive/create-permission.js @@ -46,3 +46,4 @@ if (module === require.main) { const fileId = process.argv[2]; runSample(fileId).catch(console.error); } +module.exports = runSample; From 4180444667d0c22c4a9e35c2c4bce20727074879 Mon Sep 17 00:00:00 2001 From: Justin Szaro Date: Sat, 30 Dec 2023 17:12:22 -0500 Subject: [PATCH 7/8] fix: correct copyright year in create-permission --- samples/drive/create-permission.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/drive/create-permission.js b/samples/drive/create-permission.js index 5ec42a775b..e7eb11b48f 100644 --- a/samples/drive/create-permission.js +++ b/samples/drive/create-permission.js @@ -1,4 +1,4 @@ -// Copyright 2016 Google LLC +// Copyright 2023 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at From 82be80f7741d74ead782ce1748fadfd9fbd1431a Mon Sep 17 00:00:00 2001 From: Justin Szaro Date: Sat, 6 Jan 2024 14:03:54 -0500 Subject: [PATCH 8/8] refactor: Update the copyright statement with the new year --- samples/drive/create-permission.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/drive/create-permission.js b/samples/drive/create-permission.js index e7eb11b48f..53660d0ca6 100644 --- a/samples/drive/create-permission.js +++ b/samples/drive/create-permission.js @@ -1,4 +1,4 @@ -// Copyright 2023 Google LLC +// Copyright 2024 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at