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

headless-google-auth

v0.2.1

Published

Log into a Google account using OAuth2 without a browser

Downloads

9

Readme

Log into a Google account, setting up Google's official library without a browser.

Prerequisites

Google Client ID and Secret

  1. Head to the Google Developers Console.
  2. On the dashboard, click on "Use Google APIs".
  3. On the lefthand pane, click on "Credentials".
  4. Click the dropdown labelled "New credentials". Select "OAuth Client ID".
  5. Click "Web Application". (This will show additional options.)
  6. Enter in a name. Be sure to name it something informative.
  7. Leave the "Authorized JavaScript Origins" blank for the time being.
  8. Add http://localhost:5678 as an authorized redirect URI. If you change the callback URI or port in the options (see below), make sure the new URI is added or the existing URI is altered to match.
  9. Review entries and click "Create" when finished.

Once done, you should see a popover that has the client ID and client secret. If you need to get to it again, you can click on the name selected in step 6.

NightmareJS

Nightmare is a browser automation toolkit based on Electron. Unlike v0.1.x which used Casper and consequently required several special setup steps, Nightmare is installable and usable via NPM. For most cases, it will Just Work (tm) out of the box.

However, Electron requires a video buffer to be available. If you are using this library on a headless system (eg, a server with no X client, a Docker instance, or a chroot under Crouton), you will need to use a virtual framebuffer to get this library working. More information about this can be found in the Nightmare repository's issues.

Installation

Use npm: npm install headless-google-auth

Options

The exposed method takes an options hash:

  • clientId - the client ID obtained from the Google Developers Console.
  • clientSecret - the client secret obtained from the Google Developers Console.
  • email - the email address to log into.
  • password - the password for the account.
  • scopes - scopes to request for the logging in user. Note that anything specified here is automatically granted.
  • port - (optional) the port the ad hoc NodeJS server that gets propped up for the Google callback listens on. Defaults to 5678.
  • callbackUri - (optional) the fully-qualified port-inclusive URI the Google login is going to call back to. Defaults to http://localhost:5678.
  • accessType - (optional) the kind of access to get, defaults to "online". ("offline" will get a refresh token.)

The callback (or promise resolution) passes up the newly set up Google client and the tokens used to create that client.

Usage

Callbacks

If you wanted to get a list of GMail messages (albeit only their IDs and thread IDs), for example:

var headlessAuth = require('headless-google-auth'),
    gmail = require('googleapis').gmail('v1');

headlessAuth({
    clientId: '[client ID from developer's console]',
    clientSecret: '[client secret from developer's console]',
    email: '[email protected]',
    password: 'MySuperSecretPassword',
    scopes:[
        'https://www.googleapis.com/auth/gmail.readonly',
    ]
}, function(err, client, tokens){
    gmail.users.messages.list({
        userId: '[email protected]'
        }, function(err, messages){
            //will print out an array of messages plus the next page token
            console.dir(messages);
        });
});

Promises

If you wanted tog et list of GMail messages like above, but wanted to use promises instead:

var headlessAuth = require('headless-google-auth'),
    gmail = require('googleapis').gmail('v1');

var authPromise = headlessAuth({
    clientId: '[client ID from developer's console]',
    clientSecret: '[client secret from developer's console]',
    email: '[email protected]',
    password: 'MySuperSecretPassword',
    scopes:[
        'https://www.googleapis.com/auth/gmail.readonly',
    ]
});

//... other code if needed...

authPromise.then(function(client, tokens){
    gmail.users.messages.list({
        userId: '[email protected]'
        }, function(err, messages){
            //will print out an array of messages plus the next page token
            console.dir(messages);
        });
});

Further information

Take a look at the Google NodeJS API Client for more information about scopes, authorization schemes, and other API information.

The Nightmare API documentation is also an excellent resource.

There are also other libraries that accomplish something similar to this one. Highlighting the differences:

  • Google CLI Auth - uses the open library to open the authorization link in the default (or specified) browser.
  • Google Auth CLI - very similar to the above without the ability to refresh tokens.
  • Google OAuth2 - uses PhantomJS only to acheive a similar goal, does not use the official Google API library.