Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include a create permission sample for the drive API #3411

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3336de2
feat: include a create permissions sample in the drive folder
justinszaro Dec 20, 2023
997c4aa
fix: remove unused requires and check module
justinszaro Dec 21, 2023
aed3ec6
test: add a test for create permission
justinszaro Dec 21, 2023
b2a6275
docs: add create permission example to drive samples
justinszaro Dec 30, 2023
12352d7
refactor: change file id to match the download doc example
justinszaro Dec 30, 2023
a9b3a79
refactor: add missing module.exports statement
justinszaro Dec 30, 2023
4180444
fix: correct copyright year in create-permission
justinszaro Dec 30, 2023
491c78f
Merge branch 'main' of https://github.com/justinszaro/google-api-node…
justinszaro Dec 30, 2023
098dc20
Merge branch 'main' into include-drive-permissions-sample
justinszaro Jan 4, 2024
82be80f
refactor: Update the copyright statement with the new year
justinszaro Jan 6, 2024
69d9c5d
Merge branch 'main' into include-drive-permissions-sample
justinszaro Jan 6, 2024
e37ddea
Merge branch 'main' into include-drive-permissions-sample
justinszaro Jan 17, 2024
d74ec91
Merge branch 'main' into include-drive-permissions-sample
justinszaro Feb 8, 2024
acd1d55
Merge branch 'main' into include-drive-permissions-sample
justinszaro Feb 19, 2024
599fade
Merge branch 'main' into include-drive-permissions-sample
justinszaro Feb 27, 2024
1dddd12
Merge branch 'main' into include-drive-permissions-sample
justinszaro Mar 4, 2024
b460182
Merge branch 'main' into include-drive-permissions-sample
justinszaro Mar 12, 2024
820a0e7
Merge branch 'main' into include-drive-permissions-sample
justinszaro Mar 14, 2024
297fb86
Merge branch 'main' into include-drive-permissions-sample
justinszaro Apr 2, 2024
8437e3f
Merge branch 'main' into include-drive-permissions-sample
justinszaro Apr 16, 2024
2c5a79b
Merge branch 'main' into include-drive-permissions-sample
justinszaro Apr 28, 2024
4db5aa7
Merge branch 'main' into include-drive-permissions-sample
justinszaro May 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion 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

Expand Down Expand Up @@ -67,3 +67,17 @@ Example:
```
node export.js 0B_Klegupc5gUcXhFZjZVUV9NeE0 ./file.pdf
```

__Run the `create-permission.js` sample:__

```
node create-permission.js <fileId>
```

where `<fileId>` is the id of any file in Google Drive.

Example:

```
node create-permission.js 0B_Klegupc5gUcXhFZjZVUV9NeE0
```
49 changes: 49 additions & 0 deletions samples/drive/create-permission.js
@@ -0,0 +1,49 @@
// 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
//
// 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 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);
}
module.exports = runSample;
11 changes: 11 additions & 0 deletions samples/test/test.samples.drive.js
Expand Up @@ -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)) {
Expand Down Expand Up @@ -87,4 +88,14 @@ describe('Drive samples', () => {
assert(data);
scope.done();
});

it('should create a permission', async () => {
const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA';
const scope = nock(baseUrl)
.post(`/drive/v3/files/${fileId}/permissions`)
.reply(200, {});
const data = await samples.createPermission.runSample(fileId);
assert(data);
scope.done();
});
});