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

google-offline-access

v0.0.1

Published

An opinionated way to handle offline access for Google APIs in Node.js.

Downloads

10

Readme

google-offline-access

An opinionated way to handle offline access for Google APIs in Node.js.

Conventions

This library assumes the following:

  • You have a JSON file that contains the client_id, client_secret, and redirect_uris for your application in .data/google_client_secret.json. You can get this file from the Google Cloud console.

  • The access token will be cached to .data/google_auth.json.

  • On a CI, you will have a GOOGLE_REFRESH_TOKEN environment variable set, so that access tokens can be requested without the .data/google_auth.json file.

Initialize the client

You can do this in a Node.js REPL or in your scripts:

// Import the library
const { GoogleOfflineAccess } = require('google-offline-access')

// Create a GoogleOfflineAccess instance with the required scopes.
const googleOfflineAccess = new GoogleOfflineAccess({
  scopes: [
    'https://www.googleapis.com/auth/youtube',
    'https://www.googleapis.com/auth/youtube.force-ssl',
  ],
})

Obtain a refresh token

// Get a URL to visit to obtain an authorization code.
console.log(await googleOfflineAccess.getAuthUrl())

// Once you have an authorization code, use it to obtain a refresh token.
await googleOfflineAccess.login('AUTHORIZATION_CODE')

After this step you should see a .data/google_auth.json file. It should contain the refresh_token and access_token for your application. If it doesn’t contain a refresh_token, it means that the offline access request has failed (it sometimes happen).

Using the refresh token

// Now you can obtain an authenticated AuthClient instance.
const authClient = await googleOfflineAccess.getAuthenticatedAuthClient()

// Configure googleapis to use this AuthClient instance.
const { google } = require('googleapis')
google.options({ auth: authClient })

// Try calling an API.
const youtube = google.youtube('v3')
await youtube.channels.list({ part: 'snippet', mine: true })