How to use the auth function from googleapis
Find comprehensive JavaScript googleapis.auth code examples handpicked from public code repositorys.
googleapis.auth is a module that provides a way to authenticate with Google APIs.
14 15 16 17 18 19 20 21 22 23
require('dotenv').config(); var readline = require('readline'); var google = require('googleapis'); var OAuth2Client = google.auth.OAuth2; var plus = google.plus('v1'); // Client ID and client secret are available at // https://code.google.com/apis/console
How does googleapis.auth work?
googleapis.auth is a module in the googleapis library that provides methods for authenticating with Google APIs using various types of credentials. The library supports several authentication flows, including OAuth 2.0, Service Account, and Compute Engine. The auth module provides a unified interface for obtaining and managing the necessary credentials for making authenticated API calls to Google services. It also handles refreshing expired access tokens and caching credentials to minimize the number of requests required to obtain new access tokens.
146 147 148 149 150 151 152 153 154
should redirect them to a consent page. In order to create a consent page URL: ~~~~ js var googleapis = require('googleapis'), OAuth2 = googleapis.auth.OAuth2; var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
4 5 6 7 8 9 10 11 12 13
constructor(keyPath){ var key = require(keyPath); this.jwtClient = new google.auth.JWT( key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/spreadsheets'],
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
const { google } = require("googleapis"); const key = require("./path/to/service-account-key.json"); const auth = new google.auth.GoogleAuth({ keyFile: key, scopes: "https://www.googleapis.com/auth/drive", }); const drive = google.drive({ version: "v3", auth }); drive.files.list({}, (err, res) => { if (err) { console.error("Error fetching files:", err); return; } console.log("Files:", res.data.files); });
In this example, we first require the googleapis library and load the credentials of a service account from a JSON key file. We then create a GoogleAuth instance with the loaded key and specify the scope of the API we want to access (https://www.googleapis.com/auth/drive in this case). Next, we create a client instance for the Google Drive API using the google.drive() method and passing it the API version and the authentication object. Finally, we use the drive.files.list() method to make a request to the API to list the files in the user's Google Drive account. The results of the request are passed to a callback function where we log the files to the console.
18 19 20 21 22 23 24 25 26 27
// OPTIONAL: if you want to perform authenticated requests. // You must install this dependency yourself if you need it. var google = require('googleapis'); var oauth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); // Assuming you already obtained an OAuth2 token that has access to the correct scopes somehow... oauth2Client.setCredentials({ access_token: ACCESS_TOKEN, refresh_token: REFRESH_TOKEN
GitHub: formulaslug/expense
25 26 27 28 29 30 31 32 33 34
var serviceAccountFile = 'service-account.json'; return LoadFile(serviceAccountFile).then(function(fileData) { var keyData = JSON.parse(fileData); var auth = new GoogleApis.auth.JWT( keyData.client_email, null, keyData.private_key, 'https://www.googleapis.com/auth/spreadsheets',
3 4 5 6 7 8 9 10 11 12
var Promise = require('polyfill-promise'); var request = require('request'); function authorize(options) { var authClient = new google.auth.JWT( options.email, null, options.key, [options.baseUrl]
GitHub: HR/CryptoSync
57 58 59 60 61 62 63 64 65 66
// store auth token in mdb logger.verbose(`init.drive: `) // logger.verbose(require('util').inspect(gAuth, { depth: null })) return new Promise(function (resolve, reject) { if (notInstOfAuth) { const initedGAuth = new google.auth.OAuth2(gAuth.clientId_, gAuth.clientSecret_, gAuth.redirectUri_) initedGAuth.setCredentials(gAuth.credentials) global.drive = google.drive({ version: 'v3', auth: initedGAuth
GitHub: nicholasbien/music-player
7 8 9 10 11 12 13 14 15 16 17
let jwt = require('jsonwebtoken') let request = require('request-promise') let exec = require('child_process').exec let plus = google.plus('v1') let OAuth2 = google.auth.OAuth2 let clientId = require('./secret').clientId let gSecret = require('./secret').gSecret let redirect = require('./secret').redirect let secret = require('./secret').secret
2 3 4 5 6 7 8 9 10 11 12
const google = require("googleapis").google; const cliProgress = require("cli-progress"); const colors = require("colors"); const target = ["https://www.googleapis.com/auth/drive"]; const jwt = new google.auth.JWT( process.env.GOOGLE_SERVICE_ACCOUNT_CLIENT_EMAIL, undefined, (process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY || "") .split("\\n")
52 53 54 55 56 57 58 59 60 61
return __awaiter(this, void 0, void 0, function () { var auth, authToken; return __generator(this, function (_a) { switch (_a.label) { case 0: auth = new google.auth.GoogleAuth({ keyFile: "./serviceAccountKey.json", scopes: SCOPES, }); return [4 /*yield*/, auth.getClient().catch(function (err) { return console.log("AuthToken Catch", err); })];
148 149 150 151 152 153 154 155 156 157
In `1.0`, OAuth2Client is now available at `google.auth.OAuth2` and now you can just put your `oauth2Client` right in the `auth` parameter. It's as easy as: ``` js var google = require('googleapis'); var OAuth2 = google.auth.OAuth2; // changed from google.OAuth2Client var plus = google.plus('v1'); var oauth2Client = new OAuth2(/* .. */); // initialized the same way plus.people.get({ userId: 'me', auth: oauth2Client }, callback);
71 72 73 74 75 76 77 78 79 80
//var key_path = path.join(process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE, 'key.pem'); var jwtClient = new googleapis.auth.JWT( service_account_email, key_path, null, SCOPES,
280 281 282 283 284 285 286 287 288 289
The Google Developers Console provides `.json` file that you can use to configure a JWT auth client and authenticate your requests. ``` js var key = require('path/to/key.json'); var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, [scope1, scope2], null); jwtClient.authorize(function(err, tokens) { if (err) { console.log(err);
+ 9 other calls in file
googleapis.google is the most popular function in googleapis (1113 examples)