npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-google-client

v1.1.1

Published

Simple google api client utility package

Downloads

6

Readme

simple-google-client:

Information:

This is a utility package for projects that interact with some basic Google APIs

  • This package is designed to make both authentication and interaction with Google APIs easier
  • This package handles the auth flow and token storage to make it easier to use Google APIs
  • Your OAuth tokens are encrypted and not stored in plain text
  • Changing your credentials.json file will automatically invalidate previously stored tokens
  • If possible, your existing OAuth tokens will be refreshed when they are used

Installation:

npm install simple-google-client

Usage:

1. Store your credentials file from your Google Cloud Project as credentials.json in the root directory of your project

  • You can get this file by navigating to: Google Cloud Website -> {Your Project Name} -> APIs & Services -> Credentials -> OAuth 2.0 Client IDs -> {Your Client Name} -> Actions -> Download OAuth Client -> Download JSON
  • You can manually specify a different path for this file, but I recommend keeping it within your root project directory

2. Require GoogleClient and GoogleAPI from GoogleClient:

const { GoogleClient, GoogleAPI } = require('simple-google-client');

3. Make a new instance of GoogleClient:

const client = new GoogleClient();

The constructor of GoogleClient takes the following (optional) arguments:

  • credentialsPath:
    • Type: String
    • Default: path.join(require.main.path, 'credentials.json')
    • Info: The path to your configuration file credentials.json for the OAuth process
  • scopes:
    • Type: String Array
    • Default: []
    • Info: These are the scopes that the auth process will request via OAuth, a list of which can be found here.
  • disableLogging
    • Type: Boolean
    • Default: false
    • Info: Setting this to true will disable console outputs from simple-google-client

4. Run GoogleClient.start() on your instance of GoogleClient:

client.start()
    .then(async function() {
        // Your code here
    })
    .catch(console.error);

GoogleClient.start handles the auth process and returns a promise:

  • Resolves if auth succeeds: All code in the then block can then interact with GoogleAPI without having to handle any auth
  • Rejects if auth fails: You will not be able to interact with GoogleAPI without having completed auth
  • You should only make one call to GoogleClient.start per instance

Example:

const { GoogleClient, GoogleAPI } = require('simple-google-client');
const client = new GoogleClient(['https://www.googleapis.com/auth/spreadsheets'], false);
client.start()
    .then(async function() {
        const sheetsAPI = GoogleAPI.sheets('v4');
        
        // This will set: A1=1, B1=2, A2=3, B2=4 (https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values#ValueRange)
        const sheetValues = [[1,2], [3,4]];
        
        // https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update
        const result = await sheetsAPI.spreadsheets.values.update({
            spreadsheetId: 'YourSheetId',
            range: 'Sheet1!A1:B2',
            valueInputOption: 1,
            resource: {
                values: sheetValues,
            },
        });

        console.log('Response:', result);
    })
    .catch(console.error);

Disclaimer:

  • This project utilizes googleapis and @google-cloud/local-auth, which are third-party dependencies licensed under the Apache 2.0 license.
  • This project is not affiliated with or endorsed by Google.
  • By using this package, you assume all risks and liabilities associated with its usage.
  • The developers and contributors of this project are not responsible for any damages or issues arising from the use of these dependencies.
  • It is your responsibility to review and comply with the Apache 2.0 license terms of the dependencies.

⚠️ This project was intentionally designed for a very narrow set of use-cases. I will not be implementing additional features or changes suggested via feedback!