diff --git a/package-lock.json b/package-lock.json index bc5a732c10b..00300ad93b9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dev": true, "requires": { "@types/express": "4.11.1", - "@types/node": "9.4.6" + "@types/node": "9.4.7" } }, "@types/events": { @@ -38,7 +38,7 @@ "dev": true, "requires": { "@types/events": "1.1.0", - "@types/node": "9.4.6" + "@types/node": "9.4.7" } }, "@types/glob": { @@ -49,7 +49,7 @@ "requires": { "@types/events": "1.1.0", "@types/minimatch": "3.0.3", - "@types/node": "9.4.6" + "@types/node": "9.4.7" } }, "@types/mime": { @@ -76,7 +76,7 @@ "integrity": "sha512-U5icWpv7YnZYGsN4/cmh3WD2onMY0aJIiTE6+51TwJCttdHvtCYmkBNOobHlXwrJRL0nkH9jH4kD+1FAdMN4Tg==", "dev": true, "requires": { - "@types/node": "9.4.6" + "@types/node": "9.4.7" } }, "@types/mocha": { @@ -97,7 +97,7 @@ "integrity": "sha512-TeiJ7uvv/92ugSqZ0v9l0eNXzutlki0aK+R1K5bfA5SYUil46ITlxLW4iNTCf55P4L5weCmaOdtxGeGWvudwPg==", "dev": true, "requires": { - "@types/node": "9.4.6" + "@types/node": "9.4.7" } }, "@types/nock": { @@ -106,13 +106,13 @@ "integrity": "sha512-Vdd1dRTUT5S1ONTcAMmQ2PCzIQccKMOpgu9T+knvJeGRCt29j3tcz9oRC1AM6OXD81+8U4mVuWzHklTlQW7W+w==", "dev": true, "requires": { - "@types/node": "9.4.6" + "@types/node": "9.4.7" } }, "@types/node": { - "version": "9.4.6", - "resolved": "https://registry.npmjs.org/@types/node/-/node-9.4.6.tgz", - "integrity": "sha512-CTUtLb6WqCCgp6P59QintjHWqzf4VL1uPA27bipLAPxFqrtK1gEYllePzTICGqQ8rYsCbpnsNypXjjDzGAAjEQ==", + "version": "9.4.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-9.4.7.tgz", + "integrity": "sha512-4Ba90mWNx8ddbafuyGGwjkZMigi+AWfYLSDCpovwsE63ia8w93r3oJ8PIAQc3y8U+XHcnMOHPIzNe3o438Ywcw==", "dev": true }, "@types/nunjucks": { @@ -146,7 +146,7 @@ "dev": true, "requires": { "@types/glob": "5.0.35", - "@types/node": "9.4.6" + "@types/node": "9.4.7" } }, "@types/serve-static": { @@ -165,7 +165,7 @@ "integrity": "sha512-9oVAi1Jlr274pbMGPEe0S3IPImV9knVNafa6E4MookD/fjOZAE6EmLkFX5ZjtZ9OXNPi2FCIZzUSMvwAUUKeSg==", "dev": true, "requires": { - "@types/node": "9.4.6" + "@types/node": "9.4.7" } }, "@types/string-template": { @@ -186,7 +186,7 @@ "integrity": "sha512-5fRLCYhLtDb3hMWqQyH10qtF+Ud2JnNCXTCZ+9ktNdCcgslcuXkDTkFcJNk++MT29yDntDnlF1+jD+uVGumsbw==", "dev": true, "requires": { - "@types/node": "9.4.6" + "@types/node": "9.4.7" } }, "a-sync-waterfall": { @@ -617,12 +617,6 @@ "repeat-element": "1.1.2" } }, - "browser-stdout": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.0.tgz", - "integrity": "sha1-81HTKWnTL6XXpVZxVCY9korjvR8=", - "dev": true - }, "buffer-equal-constant-time": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", @@ -3889,56 +3883,74 @@ "dev": true }, "js-green-licenses": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/js-green-licenses/-/js-green-licenses-0.4.0.tgz", - "integrity": "sha512-LpkL/9coWxNUmJ9/jxng3uvcLK6Qso2yaz7QBjO+atwtX/xmt8MrCtbkolYdhPZl/fq5dRs7Q7BLA8Ne3DKZ2w==", + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/js-green-licenses/-/js-green-licenses-0.5.0.tgz", + "integrity": "sha512-HM/SKwAl1R0y9kkBili6GqKc31ZnjzY7JHC2uO9bAHJNRmY5c/s+2cETyaqX0kBD8gX8QVhS4dJjRtY1nAwb4w==", "dev": true, "requires": { "argparse": "1.0.10", - "axios": "0.17.1", + "axios": "0.18.0", "npm-package-arg": "6.0.0", "package-json": "4.0.1", "pify": "3.0.0", - "spdx-correct": "2.0.4", - "spdx-satisfies": "0.1.3", + "spdx-correct": "3.0.0", + "spdx-satisfies": "4.0.0", "strip-json-comments": "2.0.1" }, "dependencies": { - "axios": { - "version": "0.17.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.17.1.tgz", - "integrity": "sha1-LY4+XQvb1zJ/kbyBT1xXZg+Bgk0=", + "spdx-compare": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/spdx-compare/-/spdx-compare-1.0.0.tgz", + "integrity": "sha512-C1mDZOX0hnu0ep9dfmuoi03+eOdDoz2yvK79RxbcrVEG1NO1Ph35yW102DHWKN4pk80nwCgeMmSY5L25VE4D9A==", "dev": true, "requires": { - "follow-redirects": "1.4.1", - "is-buffer": "1.1.6" + "array-find-index": "1.0.2", + "spdx-expression-parse": "3.0.0", + "spdx-ranges": "2.0.0" } }, "spdx-correct": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-2.0.4.tgz", - "integrity": "sha512-c+4gPpt9YDhz7cHlz5UrsHzxxRi4ksclxnEEKsuGT9JdwSC+ZNmsGbYRzzgxyZaBYpcWnlu+4lPcdLKx4DOCmA==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.0.0.tgz", + "integrity": "sha512-N19o9z5cEyc8yQQPukRCZ9EUmb4HUpnrmaL/fxS2pBo2jbfcFRVuFZ/oFC+vZz0MNNk0h80iMn5/S6qGZOL5+g==", "dev": true, "requires": { - "spdx-expression-parse": "2.0.2", - "spdx-license-ids": "2.0.1" + "spdx-expression-parse": "3.0.0", + "spdx-license-ids": "3.0.0" } }, "spdx-expression-parse": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/spdx-expression-parse/-/spdx-expression-parse-2.0.2.tgz", - "integrity": "sha512-oFxOkWCfFS0ltNp0H66gXlU4NF6bxg7RkoTYR0413t+yTY9zyj+AIWsjtN8dcVp6703ijDYBWBIARlJ7DkyP9Q==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/spdx-expression-parse/-/spdx-expression-parse-3.0.0.tgz", + "integrity": "sha512-Yg6D3XpRD4kkOmTpdgbUiEJFKghJH03fiC1OPll5h/0sO6neh2jqRDVHOQ4o/LMea0tgCkbMgea5ip/e+MkWyg==", "dev": true, "requires": { "spdx-exceptions": "2.1.0", - "spdx-license-ids": "2.0.1" + "spdx-license-ids": "3.0.0" } }, "spdx-license-ids": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-2.0.1.tgz", - "integrity": "sha1-AgF7zDU07k/+9tWNIOfT6aHDyOw=", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.0.tgz", + "integrity": "sha512-2+EPwgbnmOIl8HjGBXXMd9NAu02vLjOO1nWw4kmeRDFyHn+M/ETfHxQUK0oXg8ctgVnl9t3rosNVsZ1jG61nDA==", "dev": true + }, + "spdx-ranges": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/spdx-ranges/-/spdx-ranges-2.0.0.tgz", + "integrity": "sha512-AUUXLfqkwD7GlzZkXv8ePPCpPjeVWI9xJCfysL8re/uKb6H10umMnC7bFRsHmLJan4fslUtekAgpHlSgLc/7mA==", + "dev": true + }, + "spdx-satisfies": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/spdx-satisfies/-/spdx-satisfies-4.0.0.tgz", + "integrity": "sha512-OcARj6U1OuVv98SVrRqgrR30sVocONtoPpnX8Xz4vXNrFVedqtbgkA+0KmQoXIQ2xjfltPPRVIMeNzKEFLWWKQ==", + "dev": true, + "requires": { + "spdx-compare": "1.0.0", + "spdx-expression-parse": "3.0.0", + "spdx-ranges": "2.0.0" + } } } }, @@ -4388,15 +4400,15 @@ } }, "mocha": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.0.1.tgz", - "integrity": "sha512-SpwyojlnE/WRBNGtvJSNfllfm5PqEDFxcWluSIgLeSBJtXG4DmoX2NNAeEA7rP5kK+79VgtVq8nG6HskaL1ykg==", + "version": "5.0.4", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.0.4.tgz", + "integrity": "sha512-nMOpAPFosU1B4Ix1jdhx5e3q7XO55ic5a8cgYvW27CequcEY+BabS0kUVL1Cw1V5PuVHZWeNRWFLmEPexo79VA==", "dev": true, "requires": { - "browser-stdout": "1.3.0", + "browser-stdout": "1.3.1", "commander": "2.11.0", "debug": "3.1.0", - "diff": "3.3.1", + "diff": "3.5.0", "escape-string-regexp": "1.0.5", "glob": "7.1.2", "growl": "1.10.3", @@ -4405,6 +4417,12 @@ "supports-color": "4.4.0" }, "dependencies": { + "browser-stdout": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", + "dev": true + }, "commander": { "version": "2.11.0", "resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", @@ -4412,9 +4430,9 @@ "dev": true }, "diff": { - "version": "3.3.1", - "resolved": "https://registry.npmjs.org/diff/-/diff-3.3.1.tgz", - "integrity": "sha512-MKPHZDMB0o6yHyDryUOScqZibp914ksXwAMYMTHj6KO8UeKsRYNJD3oNCKjTqZon+V488P7N/HzXF8t7ZR95ww==", + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", "dev": true }, "has-flag": { @@ -7484,16 +7502,6 @@ "source-map": "0.6.1" } }, - "spdx-compare": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/spdx-compare/-/spdx-compare-0.1.2.tgz", - "integrity": "sha1-sGrz6jSvdDfZGp9Enq8tLpPDyPs=", - "dev": true, - "requires": { - "spdx-expression-parse": "1.0.4", - "spdx-ranges": "1.0.1" - } - }, "spdx-correct": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-1.0.2.tgz", @@ -7521,22 +7529,6 @@ "integrity": "sha1-yd96NCRZSt5r0RkA1ZZpbcBrrFc=", "dev": true }, - "spdx-ranges": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/spdx-ranges/-/spdx-ranges-1.0.1.tgz", - "integrity": "sha1-D07se46kjtIC43S7iULo0Y3AET4=", - "dev": true - }, - "spdx-satisfies": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/spdx-satisfies/-/spdx-satisfies-0.1.3.tgz", - "integrity": "sha1-Z6HydOYRXUquKK/kdNt2FkvhC9w=", - "dev": true, - "requires": { - "spdx-compare": "0.1.2", - "spdx-expression-parse": "1.0.4" - } - }, "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", diff --git a/samples/analytics/analytics.js b/samples/analytics/analytics.js index 9fcc838df7b..246a63278bf 100644 --- a/samples/analytics/analytics.js +++ b/samples/analytics/analytics.js @@ -14,9 +14,13 @@ 'use strict'; const {google} = require('googleapis'); -const analytics = google.analytics('v3'); const sampleClient = require('../sampleclient'); +const analytics = google.analytics({ + version: 'v3', + auth: sampleClient.oAuth2Client +}); + // Custom Goals must be exist prior to used as an objectiveMetric const objectiveMetric = 'ga:goal1Completions'; @@ -45,20 +49,23 @@ const scopes = [ 'https://www.googleapis.com/auth/analytics' ]; -sampleClient.authenticate(scopes, (err, authClient) => { - if (err) { - throw err; - } +function runSample () { analytics.management.experiments.insert({ - auth: authClient, accountId: 'your-accountId', webPropertyId: 'your-webPropertyId', profileId: 'your-profileId', resource: resourceBody - }, (err, body) => { + }, (err, res) => { if (err) { throw err; } - console.log(body); + console.log(res.data); }); +} + +sampleClient.authenticate(scopes, err => { + if (err) { + throw err; + } + runSample(); }); diff --git a/samples/blogger/blogger.js b/samples/blogger/blogger.js index 860cabd16c1..8b9928c0076 100644 --- a/samples/blogger/blogger.js +++ b/samples/blogger/blogger.js @@ -24,9 +24,9 @@ nconf.argv().env().file(path.join(__dirname, 'config.json')); blogger.blogs.get({ key: nconf.get('api_key'), blogId: 3213900 -}, (err, result) => { +}, (err, res) => { if (err) { - console.error(err); + throw err; } - console.log(result); + console.log(res.data); }); diff --git a/samples/compute/compute.js b/samples/compute/compute.js index 550e7e5f544..6c36c6d3532 100644 --- a/samples/compute/compute.js +++ b/samples/compute/compute.js @@ -33,7 +33,7 @@ request.get({ uri, headers }, (err, res, project) => { if (res.statusCode !== 200) { throw new Error(`Response failed with status ${res.statusCode}`); } - compute.zones.list({ project }, (err, result) => { - console.log(err, result); + compute.zones.list({ project }, (err, res) => { + console.log(err, res.data); }); }); diff --git a/samples/customsearch/customsearch.js b/samples/customsearch/customsearch.js index f0faddf18ab..dd686fe11cf 100644 --- a/samples/customsearch/customsearch.js +++ b/samples/customsearch/customsearch.js @@ -17,32 +17,38 @@ const {google} = require('googleapis'); const customsearch = google.customsearch('v1'); -const nconf = require('nconf'); -const path = require('path'); // Ex: node customsearch.js // "Google Node.js" -// --api_key "YOUR KEY" -// --customsearch_engine_id="YOUR ID" -nconf.argv().env().file(path.join(__dirname, 'config.json')); +// "API KEY" +// "CUSTOM ENGINE ID" -// You can get a custom search engine id at -// https://www.google.com/cse/create/new -const CX = nconf.get('customsearch_engine_id'); -const API_KEY = nconf.get('api_key'); -const SEARCH = process.argv[2]; +function runSample (options, callback) { + console.log(options); + customsearch.cse.list({ + cx: options.cx, + q: options.q, + auth: options.apiKey + }, (err, res) => { + if (err) { + throw err; + } + console.log(res.data); + callback(res.data); + }); +} -customsearch.cse.list({ - cx: CX, - q: SEARCH, - auth: API_KEY -}, (err, res) => { - if (err) { - throw err; - } - // Got the response from custom search - console.log('Result: ' + res.searchInformation.formattedTotalResults); - if (res.items && res.items.length > 0) { - console.log('First result name is ' + res.items[0].title); - } -}); +if (module === require.main) { + // You can get a custom search engine id at + // https://www.google.com/cse/create/new + const options = { + q: process.argv[2], + apiKey: process.argv[3], + cx: process.argv[4] + }; + runSample(options, () => { /* complete */ }); +} + +module.exports = { + runSample +}; diff --git a/samples/defaultauth.js b/samples/defaultauth.js index 3646e4bf591..c2acc8c7977 100644 --- a/samples/defaultauth.js +++ b/samples/defaultauth.js @@ -50,10 +50,10 @@ google.auth.getApplicationDefault((err, authClient) => { // Fetch the list of GCE zones within a project. // NOTE: You must fill in your valid project ID before running this sample! const projectId = 'fill in your project id here!'; - compute.zones.list({ project: projectId, auth: authClient }, (err, result) => { + compute.zones.list({ project: projectId, auth: authClient }, (err, res) => { if (err) { throw err; } - console.log(result); + console.log(res.data); }); }); diff --git a/samples/drive/upload.js b/samples/drive/upload.js new file mode 100644 index 00000000000..b7a9676f55d --- /dev/null +++ b/samples/drive/upload.js @@ -0,0 +1,68 @@ +// Copyright 2016, Google, Inc. +// 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 sampleClient = require('../sampleclient'); + +const drive = google.drive({ + version: 'v3', + auth: sampleClient.oAuth2Client +}); + +function runSample (fileName, callback) { + const fileSize = fs.statSync(fileName).size; + drive.files.create({ + resource: { + // a resource element is required if you want to use multipart + }, + media: { + body: fs.createReadStream(fileName) + } + }, { + // Use the `onUploadProgress` event from Axios to track the + // number of bytes uploaded to this point. + onUploadProgress: evt => { + const progress = (evt.bytesRead / fileSize) * 100; + process.stdout.clearLine(); + process.stdout.cursorTo(0); + process.stdout.write(`${Math.round(progress)}% complete`); + } + }, (err, res) => { + if (err) { + throw err; + } + console.log(res.data); + callback(res.data); + }); +} + +// if invoked directly (not tests), authenticate and run the samples +if (module === require.main) { + const scopes = ['https://www.googleapis.com/auth/drive.file']; + sampleClient.authenticate(scopes, err => { + if (err) { + throw err; + } + const fileName = process.argv[2]; + runSample(fileName, () => { /* complete */ }); + }); +} + +// export functions for testing purposes +module.exports = { + runSample, + client: sampleClient.oAuth2Client +}; diff --git a/samples/jwt.js b/samples/jwt.js index c536cb0bc19..0ff0973ab95 100644 --- a/samples/jwt.js +++ b/samples/jwt.js @@ -14,11 +14,6 @@ 'use strict'; const {google} = require('googleapis'); -const drive = google.drive('v2'); -const nconf = require('nconf'); -const path = require('path'); - -nconf.argv().env().file(path.join(__dirname, 'jwt.keys.json')); /** * The JWT authorization is ideal for performing server-to-server @@ -27,36 +22,48 @@ nconf.argv().env().file(path.join(__dirname, 'jwt.keys.json')); * Suggested reading for Admin SDK users using service accounts: * https://developers.google.com/admin-sdk/directory/v1/guides/delegation * - * Note on the private_key.pem: - * Node.js currently does not support direct access to the keys stored within - * PKCS12 file (see issue comment - * https://github.com/joyent/node/issues/4050#issuecomment-8816304) - * so the private key must be extracted and converted to a passphrase-less - * RSA key: openssl pkcs12 -in key.p12 -nodes -nocerts > key.pem - * * See the defaultauth.js sample for an alternate way of fetching compute credentials. */ -const authClient = new google.auth.JWT( - nconf.get('client_email'), - null, - nconf.get('private_key'), - // Scopes can be specified either as an array or as a single, space-delimited string - ['https://www.googleapis.com/auth/drive.readonly'], - // User to impersonate (leave empty if no impersonation needed) - 'subject-account-email@example.com'); - -authClient.authorize(err => { - if (err) { - throw err; - } - - // Make an authorized request to list Drive files. - drive.files.list({ auth: authClient }, (err, res) => { - // handle err and response +const keys = require('./jwt.keys.json'); + +// Create a new JWT client using the key file downloaded from the Google Developer Console +const jwtClient = new google.auth.JWT({ + email: keys.client_email, + key: keys.private_key, + scopes: 'https://www.googleapis.com/auth/drive.readonly' +}); + +// Obtain a new drive client, making sure you pass along the auth client +const drive = google.drive({ + version: 'v2', + auth: jwtClient +}); + +// Make an authorized request to list Drive files. +function runSample (callback) { + drive.files.list((err, res) => { if (err) { throw err; } - console.log(res); + console.log(res.data); + callback(res.data); }); -}); +} + +if (module === require.main) { + // Run `authorize` to obtain the right tokens for auth + jwtClient.authorize(err => { + if (err) { + throw err; + } + // Now that we have credentials, run the sample + runSample(() => { /* complete */ }); + }); +} + +// Exports for unit testing purposes +module.exports = { + runSample, + client: jwtClient +}; diff --git a/samples/mediaupload.js b/samples/mediaupload.js index 1a2a36f9c73..faeab67c92d 100644 --- a/samples/mediaupload.js +++ b/samples/mediaupload.js @@ -33,7 +33,7 @@ function runSamples () { if (err) { throw err; } - console.log(res); + console.log(res.data); }); // update with no metadata @@ -48,7 +48,7 @@ function runSamples () { if (err) { throw err; } - console.log(res); + console.log(res.data); }); // update example with metadata update @@ -66,7 +66,7 @@ function runSamples () { if (err) { throw err; } - console.log(res); + console.log(res.data); }); } diff --git a/samples/mirror/mirror.js b/samples/mirror/mirror.js index 8451859b204..3b905184a5e 100644 --- a/samples/mirror/mirror.js +++ b/samples/mirror/mirror.js @@ -19,6 +19,16 @@ const sampleClient = require('../sampleclient'); // initialize the Google Mirror API library const mirror = google.mirror({ version: 'v1', auth: sampleClient.oAuth2Client }); +// a very simple example of listing locations from the mirror API +function runSample () { + mirror.locations.list({}, (err, res) => { + if (err) { + throw err; + } + console.log(res.data); + }); +} + const scopes = [ 'https://www.googleapis.com/auth/glass.timeline', 'https://www.googleapis.com/auth/glass.location' @@ -28,13 +38,5 @@ sampleClient.authenticate(scopes, err => { if (err) { throw err; } - - // a very simple example of listing locations from the mirror API - mirror.locations.list({}, (err, data) => { - if (err) { - throw err; - } - console.log(data); - process.exit(); - }); + runSample(); }); diff --git a/samples/oauth2.js b/samples/oauth2.js index 0471535ed39..ce42afeb1fa 100644 --- a/samples/oauth2.js +++ b/samples/oauth2.js @@ -61,10 +61,10 @@ function getAccessToken (oauth2Client, callback) { // retrieve an access token getAccessToken(oauth2Client, () => { // retrieve user profile - plus.people.get({ userId: 'me', auth: oauth2Client }, (err, profile) => { + plus.people.get({ userId: 'me', auth: oauth2Client }, (err, res) => { if (err) { throw err; } - console.log(profile.displayName, ':', profile.tagline); + console.log(res.data.displayName, ':', res.data.tagline); }); }); diff --git a/samples/people/me.js b/samples/people/me.js index f12c76c60a9..f4a384065b8 100644 --- a/samples/people/me.js +++ b/samples/people/me.js @@ -21,15 +21,14 @@ const plus = google.plus({ auth: sampleClient.oAuth2Client }); -function me () { +function runSample () { plus.people.get({ userId: 'me' - }, (err, me) => { + }, (err, res) => { if (err) { throw err; } - console.log(me); - process.exit(); + console.log(res.data); }); } @@ -45,6 +44,6 @@ if (module === require.main) { if (err) { throw err; } - me(); + runSample(); }); } diff --git a/samples/urlshortener/urlshortener.js b/samples/urlshortener/urlshortener.js index a36e1aee033..7bb1516aeb8 100644 --- a/samples/urlshortener/urlshortener.js +++ b/samples/urlshortener/urlshortener.js @@ -22,20 +22,20 @@ const urlshortener = google.urlshortener('v1', nconf.get('api_key')); urlshortener.url.get({ shortUrl: 'http://goo.gl/DdUKX' -}, (err, result) => { +}, (err, res) => { if (err) { throw err; } - console.log(result); + console.log(res.data); }); urlshortener.url.insert({ resource: { longUrl: 'http://somelongurl.com' } -}, (err, result) => { +}, (err, res) => { if (err) { throw err; } - console.log(result); + console.log(res.data); }); diff --git a/samples/youtube/search.js b/samples/youtube/search.js index fe69f6bde9a..d05a286986b 100644 --- a/samples/youtube/search.js +++ b/samples/youtube/search.js @@ -27,12 +27,11 @@ function runSamples () { youtube.search.list({ part: 'id,snippet', q: 'Node.js on Google Cloud' - }, (err, data) => { + }, (err, res) => { if (err) { throw err; } - console.log(data); - process.exit(); + console.log(res.data); }); } diff --git a/test/samples/test.samples.auth.ts b/test/samples/test.samples.auth.ts new file mode 100644 index 00000000000..7b2fd64400a --- /dev/null +++ b/test/samples/test.samples.auth.ts @@ -0,0 +1,44 @@ +// Copyright 2018, 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. + +import * as assert from 'assert'; +import * as nock from 'nock'; +import {Utils} from './../utils'; + +nock.disableNetConnect(); + +// tslint:disable: no-any +const samples: any = { + jwt: require('../../../samples/jwt') +}; + +for (const p in samples) { + if (samples[p]) { + samples[p].client.credentials = {access_token: 'not-a-token'}; + } +} + +describe('Auth samples', () => { + afterEach(() => { + nock.cleanAll(); + }); + + it('should support JWT', done => { + const scope = nock(Utils.baseUrl).get(`/drive/v2/files`).reply(200, {}); + samples.jwt.runSample((data: {}) => { + assert(data); + scope.done(); + done(); + }); + }); +}); diff --git a/test/samples/test.samples.customsearch.ts b/test/samples/test.samples.customsearch.ts new file mode 100644 index 00000000000..46a38d0e80b --- /dev/null +++ b/test/samples/test.samples.customsearch.ts @@ -0,0 +1,40 @@ +// Copyright 2018, 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. + +import * as assert from 'assert'; +import * as nock from 'nock'; +import {Utils} from './../utils'; + +nock.disableNetConnect(); + +// tslint:disable: no-any +const samples: any = { + list: require('../../../samples/customsearch/customsearch') +}; + +describe('customsearch samples', () => { + afterEach(() => { + nock.cleanAll(); + }); + + it('should search', done => { + const scope = + nock(Utils.baseUrl).get(`/customsearch/v1?cx=cx&q=q`).reply(200, {}); + const options = {cx: 'cx', q: 'q', auth: 'key'}; + samples.list.runSample(options, (data: {}) => { + assert(data); + scope.done(); + done(); + }); + }); +}); diff --git a/test/samples/test.samples.drive.ts b/test/samples/test.samples.drive.ts index 91e7bc4704d..ba7fd26ebb4 100644 --- a/test/samples/test.samples.drive.ts +++ b/test/samples/test.samples.drive.ts @@ -25,7 +25,8 @@ nock.disableNetConnect(); const samples: any = { download: require('../../../samples/drive/download'), export: require('../../../samples/drive/export'), - list: require('../../../samples/drive/list') + list: require('../../../samples/drive/list'), + upload: require('../../../samples/drive/upload') }; for (const p in samples) { @@ -75,4 +76,15 @@ describe('Drive samples', () => { done(); }); }); + + it('should upload a file', done => { + const scope = nock(Utils.baseUrl) + .post(`/upload/drive/v3/files?uploadType=multipart`) + .reply(200, {}); + samples.upload.runSample(someFile, (data: {}) => { + assert(data); + scope.done(); + done(); + }); + }); });