# easy_google_token

> Get auth token to OAuth 2.0 for Sevice Accounts

Latest version **1.2.1** (published 2019-07-03) · ISC license · 0 weekly downloads

## Install

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

## 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.1 |
| Published | 2019-07-03 |
| First published | 2019-06-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 4 KB |
| Known vulnerabilities | 0 (+35 in 2 direct dependencies) |
| Install scripts | no |
| Author | Martin Villalba |
| Maintainers | tinchov |
| Keywords | google, oath, dialog flow, token, service account file |

## Links

- npm: https://www.npmjs.com/package/easy_google_token
- npm.io page: https://npm.io/package/easy_google_token

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^0.19.0
- [jsrsasign](https://npm.io/package/jsrsasign.md) ^8.0.12

## Alternatives

- [react-native-root-siblings](https://npm.io/package/react-native-root-siblings.md) — 102.9K weekly downloads
- [@praxisui/dialog](https://npm.io/package/@praxisui/dialog.md) — 2.0K weekly downloads
- [easy-toggle-state](https://npm.io/package/easy-toggle-state.md) — 350 weekly downloads
- [ngx-lightbox-evp](https://npm.io/package/ngx-lightbox-evp.md) — 19 weekly downloads
- [react-native-modal-translucent-axton](https://npm.io/package/react-native-modal-translucent-axton.md) — 4 weekly downloads

## Recent versions

- 1.2.1 (latest) — 2019-07-03
- 1.2.0 — 2019-07-03
- 1.1.0 — 2019-06-10

## README

# Easy Google Token

Easy Google Token is a easy way to get a token with a [Service Account File](https://cloud.google.com/iam/docs/creating-managing-service-account-keys])

_*You could use it in the Backend to get a token and use it in the Frontend or you could create a file with limited access to use it in the Frontend but I recommend the first option_

## Installing

Using npm:

```bash
$ npm install easy_google_token
```

## Using

You can check the Google scopes [HERE](https://developers.google.com/identity/protocols/googlescopes)

```js
# file: Json File with the service credentials
# scopes: String (scopes separated by comma)
# callback_function: function that will be executed when you receive the token

EasyGoogleToken.getTokenWithServiceAccountFile(file, scopes , callback_function);
```

## Example

```js
const creds = require('./service_account_file.json');
const scopes = 'https://www.googleapis.com/auth/dialogflow'
EasyGoogleToken.getTokenWithServiceAccountFile(creds, scopes, (response) => { console.log(response.data.access_token); });
```

## Example with Dialog Flow and Axios
```js
const axios = require('axios');

function sendMessage(message){
  const projectId = 'project-id';
  const session = 'random-session'
  const data = { 'queryInput': { 'text': { 'text': message, 'languageCode': 'es' } } };
  const url = `https://content-dialogflow.googleapis.com/v2/projects/${projectId}/agent/sessions/${session}:detectIntent`;
  const token = window.googleToken;

  const config = {
    headers: {
      'Authorization': `Bearer ${token}`,
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    }
  };

  return axios.post(url, data, config).then((res) => {
    res.data.queryResult.fulfillmentMessages.forEach((msg) => {
      console.log(msg);
    });
  });
}

EasyGoogleToken.getTokenWithServiceAccountFile(creds, 'https://www.googleapis.com/auth/dialogflow', function(response) {
  window.googleToken = response.data.access_token;
  sendMessage("Hello !");
});
```

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