From 7a83ae87949b746d70050a76e473f94c71faa810 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Tue, 25 Sep 2018 22:30:39 -0700 Subject: [PATCH] fix: update gsuite admin samples --- README.md | 4 +- samples/defaultauth.js | 2 +- samples/directory_v1/README.md | 41 --------------- samples/directory_v1/authData.json | 10 ---- samples/directory_v1/group-delete.js | 55 ++++++++------------ samples/directory_v1/group-email-delete.js | 57 ++++++++------------- samples/directory_v1/group-email-insert.js | 59 +++++++++------------- samples/directory_v1/group-insert.js | 57 +++++++++------------ 8 files changed, 93 insertions(+), 192 deletions(-) delete mode 100644 samples/directory_v1/README.md delete mode 100644 samples/directory_v1/authData.json diff --git a/README.md b/README.md index 0592ce2c845..2ec0bae4f5a 100644 --- a/README.md +++ b/README.md @@ -239,7 +239,7 @@ async function main () { }); // obtain the current project Id - const project = await google.auth.getDefaultProjectId(); + const project = await google.auth.getProjectId(); // Fetch the list of GCE zones within a project. const res = await compute.zones.list({ project, auth }); @@ -438,7 +438,7 @@ async function main() { scopes: ['https://www.googleapis.com/auth/cloud-platform'] }); - const projectId = await google.auth.getDefaultProjectId(); + const projectId = await google.auth.getProjectId(); const request = { projectId, diff --git a/samples/defaultauth.js b/samples/defaultauth.js index f55a4b4a5a1..e7b3f7646e5 100644 --- a/samples/defaultauth.js +++ b/samples/defaultauth.js @@ -42,7 +42,7 @@ async function main() { }); // Obtain the current project Id - const project = await google.auth.getDefaultProjectId(); + const project = await google.auth.getProjectId(); // Get the list of available compute zones for your project const res = await compute.zones.list({project, auth}); diff --git a/samples/directory_v1/README.md b/samples/directory_v1/README.md deleted file mode 100644 index 9009f1dc62d..00000000000 --- a/samples/directory_v1/README.md +++ /dev/null @@ -1,41 +0,0 @@ -Directory v_1 Examples -====================== - -This directory contains some examples how to interact with `directory v_1` API. -Authentication is done via JWT. - -### Creating a Service Account using the Google Developers Console - -1. From the [Google Developers Console](https://cloud.google.com/console), select your project or create a new one. - -2. Under "APIs & auth", click "Credentials". - -3. Under "OAuth", click the "Create new client ID" button. - -4. Select "Service account" as the application type and click "Create Client ID". - -5. The key for your new service account should prompt for download automatically. Note that your key is protected with a password. - IMPORTANT: keep a secure copy of the key, as Google keeps only the public key. - -6. Convert the downloaded key to PEM, so we can use it from the Node [crypto](http://nodejs.org/api/crypto.html) module. - - To do this, run the following in Terminal: - ```bash - openssl pkcs12 -in downloaded-key-file.p12 -out your-key-file.pem -nodes - ``` - - You will be asked for the password you received during step 5. - -That's it! You now have a service account with an email address and a key that you can use from your Node application. - -[Source. Thanks!](https://github.com/extrabacon/google-oauth-jwt) - -### Testing - -Replace the `keyFile.pem` file with the generated file generated in the previous step. Set your data in `authData.json`. - -Then you can run examples. - -```sh -$ node .js -``` diff --git a/samples/directory_v1/authData.json b/samples/directory_v1/authData.json deleted file mode 100644 index 841f91a52fe..00000000000 --- a/samples/directory_v1/authData.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "email": "21........misb7rvrmu@developer.gserviceaccount.com", - "keyFile": "./keyFile.pem", - "key": "f5938a...............68bf1a6d5ad02", - "scopes": [ - "https://www.googleapis.com/auth/admin.directory.group", - "https://www.googleapis.com/auth/admin.directory.group.member" - ], - "subject": "admin@example.com" -} diff --git a/samples/directory_v1/group-delete.js b/samples/directory_v1/group-delete.js index a0311425997..8bc134d4e3e 100644 --- a/samples/directory_v1/group-delete.js +++ b/samples/directory_v1/group-delete.js @@ -14,43 +14,30 @@ 'use strict'; const {google} = require('googleapis'); -const nconf = require('nconf'); const path = require('path'); -nconf - .argv() - .env() - .file(path.join(__dirname, '../jwt.keys.json')); +async function runSample() { + // acquire an authentication client using a service account + const auth = await google.auth.getClient({ + keyFile: path.join(__dirname, '../jwt.keys.json'), + scopes: [ + 'https://www.googleapis.com/auth/admin.directory.group', + 'https://www.googleapis.com/auth/admin.directory.group.member', + ], + }); -// Create JWT auth object -const jwt = new google.auth.JWT( - nconf.get('client_email'), - null, - nconf.get('private_key'), - [ - 'https://www.googleapis.com/auth/admin.directory.group', - 'https://www.googleapis.com/auth/admin.directory.group.member', - ] -); + // obtain the admin client + const admin = google.admin({ + version: 'directory_v1', + auth, + }); -// Authorize -jwt.authorize((err, data) => { - if (err) { - throw err; - } - console.log('You have been successfully authenticated: ', data); + // delete the group key + const res = await admin.groups.delete({ + groupKey: 'some_group@example.com', + }); - // Get Google Admin API - const admin = google.admin('directory_v1'); + console.log(res.data); +} - // Delete group - admin.groups.insert( - { - groupKey: 'some_group@example.com', - auth: jwt, - }, - (err, data) => { - console.log(err || data); - } - ); -}); +runSample().catch(console.error); diff --git a/samples/directory_v1/group-email-delete.js b/samples/directory_v1/group-email-delete.js index 78c8a0daebc..5e8ba2882c6 100644 --- a/samples/directory_v1/group-email-delete.js +++ b/samples/directory_v1/group-email-delete.js @@ -15,43 +15,30 @@ const {google} = require('googleapis'); const path = require('path'); -const nconf = require('nconf'); -nconf - .argv() - .env() - .file(path.join(__dirname, '../jwt.keys.json')); +async function runSample() { + // acquire an authentication client using a service account + const auth = await google.auth.getClient({ + keyFile: path.join(__dirname, '../jwt.keys.json'), + scopes: [ + 'https://www.googleapis.com/auth/admin.directory.group', + 'https://www.googleapis.com/auth/admin.directory.group.member', + ], + }); -// Create JWT auth object -const jwt = new google.auth.JWT( - nconf.get('client_email'), - null, - nconf.get('private_key'), - [ - 'https://www.googleapis.com/auth/admin.directory.group', - 'https://www.googleapis.com/auth/admin.directory.group.member', - ] -); + // obtain the admin client + const admin = google.admin({ + version: 'directory_v1', + auth, + }); -// Authorize -jwt.authorize((err, data) => { - if (err) { - throw err; - } - console.log('You have been successfully authenticated: ', data); + // Delete member from Google group + const res = await admin.members.delete({ + groupKey: 'my_group@example.com', + memberKey: 'me@example.com', + }); - // Get Google Admin API - const admin = google.admin('directory_v1'); + console.log(res.data); +} - // Delete member from Google group - admin.members.delete( - { - groupKey: 'my_group@example.com', - memberKey: 'me@example.com', - auth: jwt, - }, - (err, data) => { - console.log(err || data); - } - ); -}); +runSample().catch(console.error); diff --git a/samples/directory_v1/group-email-insert.js b/samples/directory_v1/group-email-insert.js index acd5ccad004..0d57c056dc3 100644 --- a/samples/directory_v1/group-email-insert.js +++ b/samples/directory_v1/group-email-insert.js @@ -15,43 +15,32 @@ const {google} = require('googleapis'); const path = require('path'); -const nconf = require('nconf'); -nconf - .argv() - .env() - .file(path.join(__dirname, '../jwt.keys.json')); +async function runSample() { + // acquire an authentication client using a service account + const auth = await google.auth.getClient({ + keyFile: path.join(__dirname, '../jwt.keys.json'), + scopes: [ + 'https://www.googleapis.com/auth/admin.directory.group', + 'https://www.googleapis.com/auth/admin.directory.group.member', + ], + }); -// Create JWT auth object -const jwt = new google.auth.JWT( - nconf.get('client_email'), - null, - nconf.get('private_key'), - [ - 'https://www.googleapis.com/auth/admin.directory.group', - 'https://www.googleapis.com/auth/admin.directory.group.member', - ] -); - -// Authorize -jwt.authorize((err, data) => { - if (err) { - throw err; - } - console.log('You have been successfully authenticated: ', data); - - // Get Google Admin API - const admin = google.admin('directory_v1'); + // obtain the admin client + const admin = google.admin({ + version: 'directory_v1', + auth, + }); // Insert member in Google group - admin.members.insert( - { - groupKey: 'my_group@example.com', - requestBody: {email: 'me@example.com'}, - auth: jwt, + const res = await admin.members.insert({ + groupKey: 'my_group@example.com', + requestBody: { + email: 'me@example.com', }, - (err, data) => { - console.log(err || data); - } - ); -}); + }); + + console.log(res.data); +} + +runSample().catch(console.error); diff --git a/samples/directory_v1/group-insert.js b/samples/directory_v1/group-insert.js index 05d80e48c9f..95d5923bd00 100644 --- a/samples/directory_v1/group-insert.js +++ b/samples/directory_v1/group-insert.js @@ -15,42 +15,31 @@ const {google} = require('googleapis'); const path = require('path'); -const nconf = require('nconf'); -nconf - .argv() - .env() - .file(path.join(__dirname, '../jwt.keys.json')); +async function runSample() { + // acquire an authentication client using a service account + const auth = await google.auth.getClient({ + keyFile: path.join(__dirname, '../jwt.keys.json'), + scopes: [ + 'https://www.googleapis.com/auth/admin.directory.group', + 'https://www.googleapis.com/auth/admin.directory.group.member', + ], + }); -// Create JWT auth object -const jwt = new google.auth.JWT( - nconf.get('client_email'), - null, - nconf.get('private_key'), - [ - 'https://www.googleapis.com/auth/admin.directory.group', - 'https://www.googleapis.com/auth/admin.directory.group.member', - ] -); - -// Authorize -jwt.authorize((err, data) => { - if (err) { - throw err; - } - console.log('You have been successfully authenticated: ', data); - - // Get Google Admin API - const admin = google.admin('directory_v1'); + // obtain the admin client + const admin = google.admin({ + version: 'directory_v1', + auth, + }); // Insert group - admin.groups.insert( - { - requestBody: {email: 'some_group@example.com'}, - auth: jwt, + const res = await admin.groups.insert({ + requestBody: { + email: 'some_group@example.com', }, - (err, data) => { - console.log(err || data); - } - ); -}); + }); + + console.log(res.data); +} + +runSample().catch(console.error);