From b55ab853459b413b9133a3c7e180f710f81a8006 Mon Sep 17 00:00:00 2001 From: Steve Bazyl Date: Tue, 5 Mar 2024 15:51:55 -0700 Subject: [PATCH] feat: Add Meet API quickstart, fix typos in others --- adminSDK/directory/index.js | 2 +- adminSDK/reports/index.js | 2 +- adminSDK/reseller/index.js | 2 +- apps-script/quickstart/index.js | 2 +- calendar/quickstart/index.js | 2 +- classroom/quickstart/index.js | 2 +- docs/quickstart/index.js | 2 +- drive/activity-v2/index.js | 2 +- drive/quickstart/index.js | 2 +- gmail/quickstart/index.js | 2 +- meet/README.md | 1 + meet/quickstart/index.js | 106 ++++++++++++++++++++++++++++++++ meet/quickstart/package.json | 18 ++++++ people/quickstart/index.js | 2 +- sheets/quickstart/index.js | 2 +- slides/quickstart/index.js | 2 +- tasks/quickstart/index.js | 2 +- 17 files changed, 139 insertions(+), 14 deletions(-) create mode 100644 meet/README.md create mode 100644 meet/quickstart/index.js create mode 100644 meet/quickstart/package.json diff --git a/adminSDK/directory/index.js b/adminSDK/directory/index.js index f0f72f84..5b1789af 100644 --- a/adminSDK/directory/index.js +++ b/adminSDK/directory/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/adminSDK/reports/index.js b/adminSDK/reports/index.js index 2453c9ce..a0a0b0d5 100644 --- a/adminSDK/reports/index.js +++ b/adminSDK/reports/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/adminSDK/reseller/index.js b/adminSDK/reseller/index.js index 727c56af..2fb10867 100644 --- a/adminSDK/reseller/index.js +++ b/adminSDK/reseller/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/apps-script/quickstart/index.js b/apps-script/quickstart/index.js index e37d37b3..52dec4ac 100644 --- a/apps-script/quickstart/index.js +++ b/apps-script/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/calendar/quickstart/index.js b/calendar/quickstart/index.js index 7e6d3aa0..88cce842 100644 --- a/calendar/quickstart/index.js +++ b/calendar/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file compatible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/classroom/quickstart/index.js b/classroom/quickstart/index.js index d7e4ab87..8564ef5f 100644 --- a/classroom/quickstart/index.js +++ b/classroom/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/docs/quickstart/index.js b/docs/quickstart/index.js index da7f61c7..269b938f 100644 --- a/docs/quickstart/index.js +++ b/docs/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/drive/activity-v2/index.js b/drive/activity-v2/index.js index 37f9e2b0..76af5269 100644 --- a/drive/activity-v2/index.js +++ b/drive/activity-v2/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/drive/quickstart/index.js b/drive/quickstart/index.js index 86f4b39e..5f0203c5 100644 --- a/drive/quickstart/index.js +++ b/drive/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/gmail/quickstart/index.js b/gmail/quickstart/index.js index 2a17bd42..3d1381ac 100644 --- a/gmail/quickstart/index.js +++ b/gmail/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file compatible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/meet/README.md b/meet/README.md new file mode 100644 index 00000000..a4523b79 --- /dev/null +++ b/meet/README.md @@ -0,0 +1 @@ +Additional samples can be found at https://github.com/googleapis/google-cloud-node/tree/main/packages/google-apps-meet diff --git a/meet/quickstart/index.js b/meet/quickstart/index.js new file mode 100644 index 00000000..8a206c23 --- /dev/null +++ b/meet/quickstart/index.js @@ -0,0 +1,106 @@ +/** + * 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 + * + * https://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. + */ +/* eslint-disable camelcase */ +// [START meet_quickstart] +const fs = require('fs').promises; +const path = require('path'); +const process = require('process'); +const {authenticate} = require('@google-cloud/local-auth'); +const {SpacesServiceClient} = require('@google-apps/meet').v2; +const { auth } = require('google-auth-library'); + +// If modifying these scopes, delete token.json. +const SCOPES = ['https://www.googleapis.com/auth/meetings.space.created']; + +// The file token.json stores the user's access and refresh tokens, and is +// created automatically when the authorization flow completes for the first +// time. +const TOKEN_PATH = path.join(process.cwd(), 'token.json'); +const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json'); + +/** + * Reads previously authorized credentials from the save file. + * + * @return {Promise} + */ +async function loadSavedCredentialsIfExist() { + try { + const content = await fs.readFile(TOKEN_PATH); + const credentials = JSON.parse(content); + return auth.fromJSON(credentials); + } catch (err) { + console.log(err); + return null; + } +} + +/** + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. + * + * @param {OAuth2Client} client + * @return {Promise} + */ +async function saveCredentials(client) { + const content = await fs.readFile(CREDENTIALS_PATH); + const keys = JSON.parse(content); + const key = keys.installed || keys.web; + const payload = JSON.stringify({ + type: 'authorized_user', + client_id: key.client_id, + client_secret: key.client_secret, + refresh_token: client.credentials.refresh_token, + }); + await fs.writeFile(TOKEN_PATH, payload); +} + +/** + * Load or request or authorization to call APIs. + * + */ +async function authorize() { + let client = await loadSavedCredentialsIfExist(); + if (client) { + return client; + } + client = await authenticate({ + scopes: SCOPES, + keyfilePath: CREDENTIALS_PATH, + }); + if (client.credentials) { + await saveCredentials(client); + } + return client; +} + +/** + * Creates a new meeting space. + * @param {OAuth2Client} authClient An authorized OAuth2 client. + */ +async function createSpace(authClient) { + const meetClient = new SpacesServiceClient({ + authClient: authClient + }); + // Construct request + const request = { + }; + + // Run request + const response = await meetClient.createSpace(request); + console.log(`Meet URL: ${response[0].meetingUri}`); +} + +authorize().then(createSpace).catch(console.error); +// [END meet_quickstart] diff --git a/meet/quickstart/package.json b/meet/quickstart/package.json new file mode 100644 index 00000000..dfb03e86 --- /dev/null +++ b/meet/quickstart/package.json @@ -0,0 +1,18 @@ +{ + "name": "google-meet-nodejs-quickstart", + "version": "1.0.0", + "description": "A simple Node.js command-line application that makes requests to the Google Meet API.", + "private": true, + "dependencies": { + "@google-cloud/local-auth": "^2.1.0", + "@google-apps/meet": "^0.1.0" + }, + "devDependencies": {}, + "engines": { + "node": ">=14" + }, + "scripts": { + "start": "node ." + }, + "license": "Apache-2.0" +} diff --git a/people/quickstart/index.js b/people/quickstart/index.js index 866475d0..75719857 100644 --- a/people/quickstart/index.js +++ b/people/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/sheets/quickstart/index.js b/sheets/quickstart/index.js index c77f5612..48550767 100644 --- a/sheets/quickstart/index.js +++ b/sheets/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/slides/quickstart/index.js b/slides/quickstart/index.js index d93b486c..320b0ba2 100644 --- a/slides/quickstart/index.js +++ b/slides/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} diff --git a/tasks/quickstart/index.js b/tasks/quickstart/index.js index 30bdae0a..fbd50763 100644 --- a/tasks/quickstart/index.js +++ b/tasks/quickstart/index.js @@ -45,7 +45,7 @@ async function loadSavedCredentialsIfExist() { } /** - * Serializes credentials to a file comptible with GoogleAUth.fromJSON. + * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise}