Skip to content
XOAuth2 token generation with node.js
Find file
Pull request Compare This branch is 1 commit ahead, 23 commits behind andris9:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


XOAuth2 token generation with node.js


npm install xoauth2


xoauth2 generates XOAUTH2 login tokens from provided Client and User credentials.

Use xoauth2.createXOAuth2Generator(options) to initialize Token Generator

Possible options values:

  • user User e-mail address
  • accessUrl Optional Endpoint for token genration (defaults to
  • clientId Client ID value
  • clientSecret Client secret value
  • refreshToken Refresh token for an user
  • accessToken Optional initial access token. If not set, a new one will be generated
  • timeout Optional timeout for the initial access token

See for generating the required credentials


Request an access token

Use xoauth2obj.getToken(callback) to get an access token. If a cached token is found and it should not be expired yet, the cached value will be used.

Request for generating a new access token

Use xoauth2obj.generateToken(callback) to get an access token. Cache will not be used and a new token is generated.

Update access token values

Use xoauth2obj.updateToken(accessToken, timeout) to set the new value for the xoauth2 access token. This function emits 'token'


If a new token value has been set, 'token' event is emitted.

xoauth2obj.on("token", function(token){
    console.log("User: ", token.user); // e-mail address
    console.log("New access token: ", token.accessToken);
    console.log("New access token timeout: ", token.timeout); // timestamp in second


var xoauth2 = require("xoauth2"),

xoauth2gen = xoauth2.createXOAuth2Generator({
    user: "",
    clientId: "{Client ID}",
    clientId: "{Client Secret}",
    refreshToken: "{User Refresh Token}"

xoauth2gen.getToken(function(err, token){
        return console.log(err);
    console.log("Base64 encoded XOAUTH2 auth string: " + token);



Something went wrong with that request. Please try again.