# xoauth2

> XOAuth2 token generation for accessing GMail SMTP and IMAP

Latest version **1.2.0** (published 2016-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install xoauth2
pnpm add xoauth2
yarn add xoauth2
bun add xoauth2
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2016-07-29 |
| First published | 2012-09-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 65 |
| Author | Andris Reinman |
| Maintainers | andris |
| Keywords | XOAUTH, XOAUTH2, Yahoo, GMail, SMTP, IMAP |

## Links

- npm: https://www.npmjs.com/package/xoauth2
- Repository: https://github.com/andris9/xoauth2
- Homepage: https://github.com/andris9/xoauth2#readme
- Issues: https://github.com/andris9/xoauth2/issues
- npm.io page: https://npm.io/package/xoauth2

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2016-07-29
- 1.1.0 — 2015-07-14
- 1.0.0 — 2014-10-13
- 0.1.8 — 2013-05-02
- 0.1.7 — 2013-03-12
- 0.1.6 — 2013-02-25
- 0.1.5 — 2013-01-28
- 0.1.4 — 2012-12-04
- 0.1.3 — 2012-10-09
- 0.1.2 — 2012-09-18
- 0.1.1 — 2012-09-10
- 0.1.0 — 2012-09-10

## README

xoauth2
=======

XOAuth2 token generation with node.js

## Installation

    npm install xoauth2

## Usage

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

Use `xoauth2.createXOAuth2Generator(options)` to initialize Token Generator

Possible options values:

  * **user** _(Required)_ User e-mail address
  * **accessUrl** _(Optional)_ Endpoint for token generation (defaults to *https://accounts.google.com/o/oauth2/token*)
  * **clientId** _(Required)_ Client ID value
  * **clientSecret** _(Required)_ Client secret value
  * **refreshToken** _(Required)_ Refresh token for an user
  * **accessToken** _(Optional)_ initial access token. If not set, a new one will be generated
  * **timeout** _(Optional)_ TTL in **seconds**
  * **customHeaders** _(Optional)_ custom headers to send during token generation request [yahoo requires `Authorization: Basic Base64(clientId:clientSecret)` ](https://developer.yahoo.com/oauth2/guide/flows_authcode/#step-5-exchange-refresh-token-for-new-access-token)
  * **customParams** _(Optional)_ custom payload to send on getToken request [yahoo requires redirect_uri to be specified](https://developer.yahoo.com/oauth2/guide/flows_authcode/#step-5-exchange-refresh-token-for-new-access-token)

See [https://developers.google.com/identity/protocols/OAuth2WebServer#offline](https://developers.google.com/identity/protocols/OAuth2WebServer#offline) for generating the required credentials

For Google service account the option values are:

  * **service** _(Required)_ Service account email.
  * **user** _(Required)_ User e-mail address
  * **scope** _(Required)_ OAuth2 scope.
  * **privateKey** _(Required)_ Private key issued for the service account in PEM format, as a string.
  * **serviceRequestTimeout** _(Optional)_ Expiration value to use in the token request in **seconds**. Maximum is 3600.
  * **accessUrl** _(Optional)_ Endpoint for token generation (defaults to *https://accounts.google.com/o/oauth2/token*)
  * **accessToken** _(Optional)_ initial access token. If not set, a new one will be generated
  * **timeout** _(Optional)_ TTL in **seconds**
  * **customHeaders** _(Optional)_ custom headers to send during token generation request
  * **customParams** _(Optional)_ custom payload to send on getToken request

### Methods

#### 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'

### Events

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); // TTL in seconds
    });

### Example

    var xoauth2 = require("xoauth2"),
        xoauth2gen;

    xoauth2gen = xoauth2.createXOAuth2Generator({
        user: "user@gmail.com",
        clientId: "{Client ID}",
        clientSecret: "{Client Secret}",
        refreshToken: "{User Refresh Token}",
        customHeaders: {
          "HeaderName": "HeaderValue"
        },
        customPayload: {
          "payloadParamName": "payloadValue"
        }
    });

    // ... or for a Google service account
    xoauth2gen = xoauth2.createXOAuth2Generator({
        user: "user@gmail.com",
        service: '{Service Email Address}',
        scope: 'https://mail.google.com/',
        privateKey: '{Private Key in PEM format}'
    });

    // SMTP/IMAP
    xoauth2gen.getToken(function(err, token){
        if(err){
            return console.log(err);
        }
        console.log("AUTH XOAUTH2 " + token);
    });

    // HTTP
    xoauth2gen.getToken(function(err, token, accessToken){
        if(err){
            return console.log(err);
        }
        console.log("Authorization: Bearer " + accessToken);
    });

## License

**MIT**

---
_Source: https://npm.io/package/xoauth2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
