diff --git a/samples/analytics/analytics.js b/samples/analytics/analytics.js index 85d14d705f7..fde570b1642 100644 --- a/samples/analytics/analytics.js +++ b/samples/analytics/analytics.js @@ -15,7 +15,6 @@ const {google} = require('googleapis'); const path = require('path'); -const {authenticate} = require('@google-cloud/local-auth'); const analytics = google.analytics('v3'); @@ -36,11 +35,12 @@ const variations = [ async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/analytics', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await analytics.management.experiments.insert({ accountId: 'your-accountId', diff --git a/samples/analyticsReporting/batchGet.js b/samples/analyticsReporting/batchGet.js index 93127dd2bed..5c5c196f45e 100644 --- a/samples/analyticsReporting/batchGet.js +++ b/samples/analyticsReporting/batchGet.js @@ -15,17 +15,17 @@ const {google} = require('googleapis'); const path = require('path'); -const {authenticate} = require('@google-cloud/local-auth'); const analyticsreporting = google.analyticsreporting('v4'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/analytics', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await analyticsreporting.reports.batchGet({ requestBody: { diff --git a/samples/blogger/insert.js b/samples/blogger/insert.js index f8afa5c2afc..e688027e282 100644 --- a/samples/blogger/insert.js +++ b/samples/blogger/insert.js @@ -15,17 +15,17 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const blogger = google.blogger('v3'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/blogger', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await blogger.posts.insert({ blogId: '4340475495955554224', diff --git a/samples/docs/create.js b/samples/docs/create.js index afec7fe02ba..51282594433 100644 --- a/samples/docs/create.js +++ b/samples/docs/create.js @@ -15,17 +15,17 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const docs = google.docs('v1'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/documents', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); // The initial call to create the doc will have a title but no content. // This is a limitation of the underlying API. diff --git a/samples/docs/get.js b/samples/docs/get.js index 480530730a1..3e17a596f71 100644 --- a/samples/docs/get.js +++ b/samples/docs/get.js @@ -16,17 +16,17 @@ const path = require('path'); const util = require('util'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const docs = google.docs('v1'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/documents', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await docs.documents.get({ documentId: '1XPbMENiP5bWP_cbqc0bEWbq78vmUf-rWQ6aB6FVZJyc', diff --git a/samples/drive/download.js b/samples/drive/download.js index ded4075cdfd..5963f790878 100644 --- a/samples/drive/download.js +++ b/samples/drive/download.js @@ -17,7 +17,6 @@ const {google} = require('googleapis'); const fs = require('fs'); const os = require('os'); const path = require('path'); -const {authenticate} = require('@google-cloud/local-auth'); const crypto = require('crypto'); const uuid = crypto.randomUUID(); @@ -25,7 +24,7 @@ const drive = google.drive('v3'); async function runSample(fileId) { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://www.googleapis.com/auth/drive', @@ -37,7 +36,8 @@ async function runSample(fileId) { 'https://www.googleapis.com/auth/drive.readonly', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); // For converting document formats, and for downloading template // documents, see the method drive.files.export(): diff --git a/samples/drive/export.js b/samples/drive/export.js index da9d0f01d37..b5f104e71c2 100644 --- a/samples/drive/export.js +++ b/samples/drive/export.js @@ -17,18 +17,18 @@ const {google} = require('googleapis'); const fs = require('fs'); const os = require('os'); const path = require('path'); -const {authenticate} = require('@google-cloud/local-auth'); const drive = google.drive('v3'); async function runSample() { // [START main_body] // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/drive.readonly', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA'; const destPath = path.join(os.tmpdir(), 'important.pdf'); diff --git a/samples/drive/list.js b/samples/drive/list.js index b0dfb9d95e1..3e56ccd6ce2 100644 --- a/samples/drive/list.js +++ b/samples/drive/list.js @@ -15,17 +15,17 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const drive = google.drive('v3'); async function runSample(query) { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/drive.metadata.readonly', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const params = {pageSize: 3}; params.q = query; diff --git a/samples/drive/quickstart.js b/samples/drive/quickstart.js index 1f140f0d673..09cef9ace06 100644 --- a/samples/drive/quickstart.js +++ b/samples/drive/quickstart.js @@ -15,7 +15,6 @@ // [START main_body] const path = require('path'); -const {authenticate} = require('@google-cloud/local-auth'); const {google} = require('googleapis'); /** @@ -23,11 +22,12 @@ const {google} = require('googleapis'); */ async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/drive.metadata.readonly', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const service = google.drive('v3'); const res = await service.files.list({ @@ -43,6 +43,7 @@ async function runSample() { console.log(`${file.name} (${file.id})`); } } + return res.data; } if (module === require.main) { runSample().catch(console.error); diff --git a/samples/drive/upload.js b/samples/drive/upload.js index 33e997439c7..ee5bdd91190 100644 --- a/samples/drive/upload.js +++ b/samples/drive/upload.js @@ -17,17 +17,17 @@ const fs = require('fs'); const path = require('path'); const readline = require('readline'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const drive = google.drive('v3'); async function runSample(fileName) { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/drive.file', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const fileSize = fs.statSync(fileName).size; const res = await drive.files.create( diff --git a/samples/gmail/labels.js b/samples/gmail/labels.js index f2870183d90..d2d704b0183 100644 --- a/samples/gmail/labels.js +++ b/samples/gmail/labels.js @@ -15,17 +15,17 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const gmail = google.gmail('v1'); async function runSample(action, messageId, labelId) { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/gmail.modify', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); if (action === 'add') { const res = await gmail.users.messages.modify({ diff --git a/samples/gmail/list.js b/samples/gmail/list.js index 951adcc2886..41ec54bdb1e 100644 --- a/samples/gmail/list.js +++ b/samples/gmail/list.js @@ -15,17 +15,17 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const gmail = google.gmail('v1'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: 'https://www.googleapis.com/auth/gmail.readonly', }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await gmail.users.messages.list({userId: 'me'}); console.log(res.data); diff --git a/samples/gmail/send.js b/samples/gmail/send.js index acaabdb5cd0..329a963153c 100644 --- a/samples/gmail/send.js +++ b/samples/gmail/send.js @@ -15,13 +15,12 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const gmail = google.gmail('v1'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://mail.google.com/', @@ -30,7 +29,8 @@ async function runSample() { 'https://www.googleapis.com/auth/gmail.send', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); // You can use UTF-8 encoding for the subject using the method below. // You can also just use a plain string if you don't need anything fancy. diff --git a/samples/gmail/watch.js b/samples/gmail/watch.js index 97afcd0f4f8..3b0b1668648 100644 --- a/samples/gmail/watch.js +++ b/samples/gmail/watch.js @@ -15,7 +15,6 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const gmail = google.gmail('v1'); @@ -32,7 +31,7 @@ const gmail = google.gmail('v1'); */ async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://mail.google.com/', @@ -41,7 +40,8 @@ async function runSample() { 'https://www.googleapis.com/auth/gmail.readonly', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await gmail.users.watch({ userId: 'me', diff --git a/samples/jobs/jobs.js b/samples/jobs/jobs.js index 4a333d94c04..b90ffaa2c1e 100644 --- a/samples/jobs/jobs.js +++ b/samples/jobs/jobs.js @@ -15,20 +15,20 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const jobService = google.jobs('v3'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://www.googleapis.com/auth/jobs', 'https://www.googleapis.com/auth/cloud-platform', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const projectId = await google.auth.getProjectId(); const res = await jobService.projects.companies.create({ diff --git a/samples/mediaupload.js b/samples/mediaupload.js index 4a3cc14f4af..aff2268937a 100644 --- a/samples/mediaupload.js +++ b/samples/mediaupload.js @@ -15,13 +15,12 @@ const {google} = require('googleapis'); const path = require('path'); -const {authenticate} = require('@google-cloud/local-auth'); const drive = google.drive('v3'); async function runSamples() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://www.googleapis.com/auth/drive.metadata', @@ -29,7 +28,8 @@ async function runSamples() { 'https://www.googleapis.com/auth/drive', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); // insertion example let res = await drive.files.insert({ diff --git a/samples/mirror/mirror.js b/samples/mirror/mirror.js index 5538ac21027..8166c6ee638 100644 --- a/samples/mirror/mirror.js +++ b/samples/mirror/mirror.js @@ -15,7 +15,6 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); // initialize the Google Mirror API library const mirror = google.mirror('v1'); @@ -23,14 +22,15 @@ const mirror = google.mirror('v1'); // a very simple example of listing locations from the mirror API async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://www.googleapis.com/auth/glass.timeline', 'https://www.googleapis.com/auth/glass.location', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await mirror.locations.list({}); console.log(res.data); diff --git a/samples/package.json b/samples/package.json index bcfd1a95ab3..682a6039c77 100644 --- a/samples/package.json +++ b/samples/package.json @@ -16,7 +16,6 @@ "test": "mocha" }, "dependencies": { - "@google-cloud/local-auth": "^3.0.0", "express": "^5.0.0", "googleapis": "^168.0.0", "googleapis-common": "^8.0.2-rc.0", diff --git a/samples/people/contacts.js b/samples/people/contacts.js index 3c95937f238..90c6a6ce3cd 100644 --- a/samples/people/contacts.js +++ b/samples/people/contacts.js @@ -15,17 +15,17 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const people = google.people('v1'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: ['https://www.googleapis.com/auth/contacts'], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); // List all user's contact groups // https://developers.google.com/people/api/rest/v1/contactGroups diff --git a/samples/people/me.js b/samples/people/me.js index efc051f5759..bac1e4488e0 100644 --- a/samples/people/me.js +++ b/samples/people/me.js @@ -15,20 +15,20 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const people = google.people('v1'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); // See documentation of personFields at // https://developers.google.com/people/api/rest/v1/people/get diff --git a/samples/sheets/append.js b/samples/sheets/append.js index 3e94ef118d9..479d2f69019 100644 --- a/samples/sheets/append.js +++ b/samples/sheets/append.js @@ -15,13 +15,12 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const sheets = google.sheets('v4'); async function runSample(spreadsheetId, range) { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://www.googleapis.com/auth/drive', @@ -29,7 +28,8 @@ async function runSample(spreadsheetId, range) { 'https://www.googleapis.com/auth/spreadsheets', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await sheets.spreadsheets.values.append({ spreadsheetId, diff --git a/samples/sheets/insert-column.js b/samples/sheets/insert-column.js index bf1edfbd306..a1f0002cef6 100644 --- a/samples/sheets/insert-column.js +++ b/samples/sheets/insert-column.js @@ -15,17 +15,17 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const sheets = google.sheets('v4'); async function runSample(spreadsheetId, sheetId, startIndex, endIndex) { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: ['https://www.googleapis.com/auth/spreadsheets'], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await sheets.spreadsheets.batchUpdate({ spreadsheetId, @@ -46,6 +46,7 @@ async function runSample(spreadsheetId, sheetId, startIndex, endIndex) { }, }); console.info(res); + return res.data; } if (module === require.main) { diff --git a/samples/test/common.js b/samples/test/common.js new file mode 100644 index 00000000000..780e7e52a73 --- /dev/null +++ b/samples/test/common.js @@ -0,0 +1,43 @@ +// Copyright 2025 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'); + +function getStubs() { + return { + 'googleapis': { + google: { + ...google, + options: () => {}, + auth: { + ...google.auth, + GoogleAuth: class { + constructor() { + return { + getClient: async () => { + const client = new google.auth.OAuth2(); + client.credentials = {access_token: 'not-a-token'}; + return client; + } + } + } + }, + }, + } + } + }; +} + +module.exports = { getStubs }; diff --git a/samples/test/test.samples.analyticsReporting.js b/samples/test/test.samples.analyticsReporting.js index 47e98d44674..9a07a84cc8c 100644 --- a/samples/test/test.samples.analyticsReporting.js +++ b/samples/test/test.samples.analyticsReporting.js @@ -18,20 +18,17 @@ const {describe, it, afterEach} = require('mocha'); const nock = require('nock'); const proxyquire = require('proxyquire'); const {google} = require('googleapis'); +const {getStubs} = require('./common.js'); nock.disableNetConnect(); const baseUrl = 'https://analyticsreporting.googleapis.com'; +const stubs = getStubs(); + describe('analyticsReporting samples', () => { - const batchGet = proxyquire('../analyticsReporting/batchGet', { - '@google-cloud/local-auth': { - authenticate: async () => { - const client = new google.auth.OAuth2(); - client.credentials = {access_token: 'not-a-token'}; - }, - }, - }); + const batchGet = proxyquire('../analyticsReporting/batchGet', stubs); + afterEach(() => { nock.cleanAll(); diff --git a/samples/test/test.samples.blog.js b/samples/test/test.samples.blog.js index 31a55da44fa..f66d3ffdd4d 100644 --- a/samples/test/test.samples.blog.js +++ b/samples/test/test.samples.blog.js @@ -18,20 +18,17 @@ const nock = require('nock'); const {describe, it, afterEach} = require('mocha'); const proxyquire = require('proxyquire'); const {google} = require('googleapis'); +const {getStubs} = require('./common.js'); nock.disableNetConnect(); const baseUrl = 'https://blogger.googleapis.com'; +const stubs = getStubs(); + describe('blogger samples', () => { - const insert = proxyquire('../blogger/insert', { - '@google-cloud/local-auth': { - authenticate: async () => { - const client = new google.auth.OAuth2(); - client.credentials = {access_token: 'not-a-token'}; - }, - }, - }); + const insert = proxyquire('../blogger/insert', stubs); + afterEach(() => { nock.cleanAll(); diff --git a/samples/test/test.samples.docs.js b/samples/test/test.samples.docs.js index 2d87e4b39d2..120d4f07991 100644 --- a/samples/test/test.samples.docs.js +++ b/samples/test/test.samples.docs.js @@ -17,6 +17,7 @@ const assert = require('assert'); const nock = require('nock'); const {describe, it, afterEach} = require('mocha'); const {google} = require('googleapis'); +const {getStubs} = require('./common.js'); const proxyquire = require('proxyquire'); nock.disableNetConnect(); @@ -28,15 +29,10 @@ const samples = { const baseUrl = 'https://docs.googleapis.com'; +const stubs = getStubs(); + for (const sample of Object.values(samples)) { - sample.runSample = proxyquire(sample.path, { - '@google-cloud/local-auth': { - authenticate: async () => { - const client = new google.auth.OAuth2(); - client.credentials = {access_token: 'not-a-token'}; - }, - }, - }); + sample.runSample = proxyquire(sample.path, stubs); } describe('docs samples', () => { diff --git a/samples/test/test.samples.drive.js b/samples/test/test.samples.drive.js index 48c7bdc3e3b..fd66a643336 100644 --- a/samples/test/test.samples.drive.js +++ b/samples/test/test.samples.drive.js @@ -21,6 +21,7 @@ const path = require('path'); const {describe, it, afterEach} = require('mocha'); const proxyquire = require('proxyquire'); const {google} = require('googleapis'); +const {getStubs} = require('./common.js'); nock.disableNetConnect(); @@ -29,17 +30,13 @@ const samples = { export: {path: '../drive/export'}, list: {path: '../drive/list'}, upload: {path: '../drive/upload'}, + quickstart: {path: '../drive/quickstart'}, }; +const stubs = getStubs(); + for (const sample of Object.values(samples)) { - sample.runSample = proxyquire(sample.path, { - '@google-cloud/local-auth': { - authenticate: async () => { - const client = new google.auth.OAuth2(); - client.credentials = {access_token: 'not-a-token'}; - }, - }, - }); + sample.runSample = proxyquire(sample.path, stubs); } const someFile = path.join(__dirname, '../../test/fixtures/public.pem'); @@ -50,6 +47,15 @@ describe('Drive samples', () => { nock.cleanAll(); }); + it('should list files in quickstart', async () => { + const scope = nock(baseUrl) + .get('/drive/v3/files?pageSize=10&fields=nextPageToken%2C%20files(id%2C%20name)') + .reply(200, { files: [] }); + const data = await samples.quickstart.runSample(); + assert.deepStrictEqual(data, { files: [] }); + scope.done(); + }); + it('should download the file', async () => { const fileId = '0B7l5uajXUzaFa0x6cjJfZEkzZVE'; const scope = nock(baseUrl) diff --git a/samples/test/test.samples.gmail.js b/samples/test/test.samples.gmail.js index a4ef146c69c..bbfd0ba3ddb 100644 --- a/samples/test/test.samples.gmail.js +++ b/samples/test/test.samples.gmail.js @@ -18,6 +18,7 @@ const nock = require('nock'); const {describe, it, afterEach} = require('mocha'); const proxyquire = require('proxyquire'); const {google} = require('googleapis'); +const {getStubs} = require('./common.js'); nock.disableNetConnect(); @@ -28,15 +29,10 @@ const samples = { send: {path: '../gmail/send'}, }; +const stubs = getStubs(); + for (const sample of Object.values(samples)) { - sample.runSample = proxyquire(sample.path, { - '@google-cloud/local-auth': { - authenticate: async () => { - const client = new google.auth.OAuth2(); - client.credentials = {access_token: 'not-a-token'}; - }, - }, - }); + sample.runSample = proxyquire(sample.path, stubs); } const gmailUrl = 'https://gmail.googleapis.com'; diff --git a/samples/test/test.samples.sheets.js b/samples/test/test.samples.sheets.js index 88e95ff645b..a1a38872635 100644 --- a/samples/test/test.samples.sheets.js +++ b/samples/test/test.samples.sheets.js @@ -18,6 +18,7 @@ const nock = require('nock'); const {describe, it, afterEach} = require('mocha'); const proxyquire = require('proxyquire'); const {google} = require('googleapis'); +const {getStubs} = require('./common.js'); const baseUrl = 'https://sheets.googleapis.com'; @@ -25,17 +26,13 @@ nock.disableNetConnect(); const samples = { append: {path: '../sheets/append'}, + 'insert-column': {path: '../sheets/insert-column'}, }; +const stubs = getStubs(); + for (const sample of Object.values(samples)) { - sample.runSample = proxyquire(sample.path, { - '@google-cloud/local-auth': { - authenticate: async () => { - const client = new google.auth.OAuth2(); - client.credentials = {access_token: 'not-a-token'}; - }, - }, - }); + sample.runSample = proxyquire(sample.path, stubs); } describe('sheets samples', () => { @@ -43,6 +40,17 @@ describe('sheets samples', () => { nock.cleanAll(); }); + it('should insert a column', async () => { + const scope = nock(baseUrl) + .post( + `/v4/spreadsheets/aSheetId:batchUpdate`, + ) + .reply(200, {}); + const data = await samples['insert-column'].runSample('aSheetId', 'aSheetId', 1, 2); + assert(data); + scope.done(); + }); + it('should append values', async () => { const range = 'A1:A10'; const scope = nock(baseUrl) diff --git a/samples/test/test.samples.webmasters.js b/samples/test/test.samples.webmasters.js index 5c62fe8e93d..edfbf11d7ea 100644 --- a/samples/test/test.samples.webmasters.js +++ b/samples/test/test.samples.webmasters.js @@ -18,6 +18,7 @@ const nock = require('nock'); const {describe, it, afterEach} = require('mocha'); const proxyquire = require('proxyquire'); const {google} = require('googleapis'); +const {getStubs} = require('./common.js'); nock.disableNetConnect(); @@ -25,15 +26,10 @@ const samples = { query: {path: '../webmasters/query'}, }; +const stubs = getStubs(); + for (const sample of Object.values(samples)) { - sample.runSample = proxyquire(sample.path, { - '@google-cloud/local-auth': { - authenticate: async () => { - const client = new google.auth.OAuth2(); - client.credentials = {access_token: 'not-a-token'}; - }, - }, - }); + sample.runSample = proxyquire(sample.path, stubs); } describe('webmaster samples', () => { diff --git a/samples/test/test.samples.youtube.js b/samples/test/test.samples.youtube.js index eab6451b088..685ebb3d396 100644 --- a/samples/test/test.samples.youtube.js +++ b/samples/test/test.samples.youtube.js @@ -19,22 +19,20 @@ const nock = require('nock'); const path = require('path'); const proxyquire = require('proxyquire'); const {google} = require('googleapis'); +const {getStubs} = require('./common.js'); nock.disableNetConnect(); const samples = { upload: {path: '../youtube/upload'}, + playlist: {path: '../youtube/playlist'}, + search: {path: '../youtube/search'}, }; +const stubs = getStubs(); + for (const sample of Object.values(samples)) { - sample.runSample = proxyquire(sample.path, { - '@google-cloud/local-auth': { - authenticate: async () => { - const client = new google.auth.OAuth2(); - client.credentials = {access_token: 'not-a-token'}; - }, - }, - }); + sample.runSample = proxyquire(sample.path, stubs); } const someFile = path.join(__dirname, '../../test/fixtures/public.pem'); @@ -44,6 +42,38 @@ describe('YouTube samples', () => { nock.cleanAll(); }); + it('should get playlist data', async () => { + const scope = nock('https://youtube.googleapis.com') + .get( + '/youtube/v3/playlists?part=id%2Csnippet&id=PLIivdWyY5sqIij_cgINUHZDMnGjVx3rxi' + ) + .reply(200, { + kind: 'youtube#playlistListResponse', + etag: 'a-real-etag', + }) + .get( + '/youtube/v3/playlists?part=id%2Csnippet&id=PLIivdWyY5sqIij_cgINUHZDMnGjVx3rxi' + ) + .matchHeader('If-None-Match', 'a-real-etag') + .reply(304); + const res = await samples.playlist.runSample(); + assert(res.data); + assert.strictEqual(res.data.kind, 'youtube#playlistListResponse'); + scope.done(); + }); + + it('should search for videos', async () => { + const scope = nock('https://youtube.googleapis.com') + .get( + '/youtube/v3/search?part=id%2Csnippet&q=Node.js%20on%20Google%20Cloud' + ) + .reply(200, {kind: 'youtube#searchListResponse'}); + const data = await samples.search.runSample(); + assert(data); + assert.strictEqual(data.kind, 'youtube#searchListResponse'); + scope.done(); + }); + it('should upload a video', async () => { const scope = nock('https://youtube.googleapis.com') .post( diff --git a/samples/webmasters/query.js b/samples/webmasters/query.js index e5b462ecbef..5fea3620947 100644 --- a/samples/webmasters/query.js +++ b/samples/webmasters/query.js @@ -15,20 +15,20 @@ const path = require('path'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); const webmasters = google.webmasters('v3'); async function runSample() { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://www.googleapis.com/auth/webmasters', 'https://www.googleapis.com/auth/webmasters.readonly', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await webmasters.searchanalytics.query({ siteUrl: 'http://jbeckwith.com', diff --git a/samples/youtube/playlist.js b/samples/youtube/playlist.js index 7c135dd719e..437f480f6ad 100644 --- a/samples/youtube/playlist.js +++ b/samples/youtube/playlist.js @@ -15,18 +15,18 @@ const {google} = require('googleapis'); const path = require('path'); -const {authenticate} = require('@google-cloud/local-auth'); // initialize the Youtube API library const youtube = google.youtube('v3'); // a very simple example of getting data from a playlist async function runSample() { - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: ['https://www.googleapis.com/auth/youtube'], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); // the first query will return data with an etag const res = await getPlaylistData(null); @@ -37,6 +37,7 @@ async function runSample() { // since the If-None-Match header was set with a matching eTag const res2 = await getPlaylistData(etag); console.log(res2.status); + return res; } async function getPlaylistData(etag) { diff --git a/samples/youtube/search.js b/samples/youtube/search.js index 13fff40a360..70f55264253 100644 --- a/samples/youtube/search.js +++ b/samples/youtube/search.js @@ -15,24 +15,25 @@ const {google} = require('googleapis'); const path = require('path'); -const {authenticate} = require('@google-cloud/local-auth'); // initialize the Youtube API library const youtube = google.youtube('v3'); // a very simple example of searching for youtube videos async function runSample() { - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: ['https://www.googleapis.com/auth/youtube'], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const res = await youtube.search.list({ part: 'id,snippet', q: 'Node.js on Google Cloud', }); console.log(res.data); + return res.data; } if (module === require.main) { diff --git a/samples/youtube/upload.js b/samples/youtube/upload.js index cfea48587ef..43cf09fbdce 100644 --- a/samples/youtube/upload.js +++ b/samples/youtube/upload.js @@ -21,7 +21,6 @@ const fs = require('fs'); const path = require('path'); const readline = require('readline'); const {google} = require('googleapis'); -const {authenticate} = require('@google-cloud/local-auth'); // initialize the Youtube API library const youtube = google.youtube('v3'); @@ -29,14 +28,15 @@ const youtube = google.youtube('v3'); // very basic example of uploading a video to youtube async function runSample(fileName) { // Obtain user credentials to use for the request - const auth = await authenticate({ + const auth = new google.auth.GoogleAuth({ keyfilePath: path.join(__dirname, '../oauth2.keys.json'), scopes: [ 'https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube', ], }); - google.options({auth}); + const client = await auth.getClient(); + google.options({auth: client}); const fileSize = fs.statSync(fileName).size; const res = await youtube.videos.insert( diff --git a/src/googleapis.ts b/src/googleapis.ts index 9881061db70..9d0153c6360 100644 --- a/src/googleapis.ts +++ b/src/googleapis.ts @@ -1,4 +1,4 @@ -// Copyright 2012 Google LLC +// Copyright 2025 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 diff --git a/src/index.ts b/src/index.ts index e2a59922bfe..68fdefdf2b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// Copyright 2025 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 diff --git a/system-test/auth.test.ts b/system-test/auth.test.ts index 276d1758586..3cea68771e0 100644 --- a/system-test/auth.test.ts +++ b/system-test/auth.test.ts @@ -53,7 +53,7 @@ describe('google.auth', async () => { }); const vms = result.data; assert.strictEqual(typeof vms.kind, 'string'); - }); + }).timeout(20000); }); describe('new google.auth.GoogleAuth', async () => {