Socket
Socket
Sign inDemoInstall

google-auth-token-generator

Package Overview
Dependencies
84
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    google-auth-token-generator

[![Image from Gyazo](https://i.gyazo.com/92b73b1253594a5c39761079492cb9da.gif)](https://gyazo.com/92b73b1253594a5c39761079492cb9da)


Version published
Weekly downloads
310
decreased by-7.74%
Maintainers
1
Install size
110 MB
Created
Weekly downloads
 

Readme

Source

Image from Gyazo

google-auth-token-generator

Gooogle系APIのtoken.jsonを取得するためのCLIツールです。 npx経由で実行すればインストール不要です。

  • Google Drive
  • Gmail
  • Google Calender
  • Youtube Data API
  • Google Sheets

に対応しています。(2021/5/3時点)

使い方

    1. CLIツールとして、token.jsonの生成が出来ます。
    1. oAuthClientの生成関数のモジュールとして利用できます。

1. token.jsonの生成

    1. Googleのサイトから利用したいAPIのcredentials.jsonを取得します。
    1. npx google-auth-token-generatorを実行します。
    1. token.jsonが生成されます

2. oAuthClientの生成モジュール

読み込みます。googleapisも追加でインストールしましょう。

const {google} = require('googleapis');
const {oAuth2ClientGen} = require('google-auth-token-generator');

利用する際は以下のように読み込みます。 ファイル名は任意に変更できますがcredentials.jsonとtoken.jsonがデフォルトです。

const auth = oAuth2ClientGen({
  library: google,
  /*ファイル利用の場合*/
  CREDENTIALS_PATH: `credentials.json`, // optional
  TOKEN_PATH: 'token.json'; // optional
});

また、環境変数などを利用したい場合はCREDENTIALSとTOKENのキーにセットして下さい。ファイル読み込みはしません。

const auth = oAuth2ClientGen({
  library: google,
  /*環境変数利用の場合*/
  CREDENTIALS: process.env.GOOGLE_CREDENTIAL, //optional
  TOKEN: process.env.GOOGLE_TOKEN,  //optional
});
Google Spread SheetsのAPI利用サンプル
'use strict';

const {google} = require('googleapis');
const {oAuth2ClientGen} = require('google-auth-token-generator');

(async () => {
    const auth = oAuth2ClientGen({library: google});
    const sheets = google.sheets({version: 'v4', auth});

    try {
        const res = await sheets.spreadsheets.values.get({
            spreadsheetId: '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms',
            range: 'Class Data!A2:E',
        });

        const rows = res.data.values;
        if (rows.length) {
          console.log('Name, Major:');
          // Print columns A and E, which correspond to indices 0 and 4.
          rows.map((row) => {
            console.log(`${row[0]}, ${row[4]}`);
          });
        } else {
          console.log('No data found.');
        }

    } catch (error) {
        console.log('The API returned an error: ' + error);
    }
})();

how to use

    1. Get credentials.json first from the Google API site.
    1. run npx google-auth-token-generator
    1. A token.json file will be generated.

Keywords

FAQs

Last updated on 11 Apr 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc