{"id":13479435,"url":"https://github.com/googleapis/node-gtoken","last_synced_at":"2025-10-23T20:06:26.824Z","repository":{"id":22963381,"uuid":"26313215","full_name":"googleapis/node-gtoken","owner":"googleapis","description":":key: Google Auth Service Account Tokens for Node.js ","archived":false,"fork":false,"pushed_at":"2025-03-26T19:54:46.000Z","size":690,"stargazers_count":113,"open_issues_count":8,"forks_count":44,"subscribers_count":45,"default_branch":"main","last_synced_at":"2025-03-28T15:08:26.762Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/googleapis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-11-07T09:37:40.000Z","updated_at":"2025-03-05T18:16:56.000Z","dependencies_parsed_at":"2024-02-01T05:27:47.128Z","dependency_job_id":"9afa1438-db7b-477c-9e3c-232836f6cfcc","html_url":"https://github.com/googleapis/node-gtoken","commit_stats":{"total_commits":380,"total_committers":38,"mean_commits":10.0,"dds":0.8078947368421052,"last_synced_commit":"6ec11f26ad637f92438269e37efceaafbbad022b"},"previous_names":["google/node-gtoken"],"tags_count":47,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googleapis%2Fnode-gtoken","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googleapis%2Fnode-gtoken/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googleapis%2Fnode-gtoken/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googleapis%2Fnode-gtoken/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/googleapis","download_url":"https://codeload.github.com/googleapis/node-gtoken/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247128725,"owners_count":20888235,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T16:02:16.052Z","updated_at":"2025-10-23T20:06:26.770Z","avatar_url":"https://github.com/googleapis.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003cimg src=\"https://avatars2.githubusercontent.com/u/2810941?v=3\u0026s=96\" alt=\"Google Cloud Platform logo\" title=\"Google Cloud Platform\" align=\"right\" height=\"96\" width=\"96\"/\u003e\n\n# [node-gtoken](https://github.com/googleapis/node-gtoken)\n\n[![npm version][npm-image]][npm-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![codecov][codecov-image]][codecov-url]\n[![Code Style: Google][gts-image]][gts-url]\n\n\u003e Node.js Google Authentication Service Account Tokens\n\nThis is a low level utility library used to interact with Google Authentication services.  **In most cases, you probably want to use the [google-auth-library](https://github.com/googleapis/google-auth-library-nodejs) instead.**\n\n* [gtoken API Reference][client-docs]\n* [github.com/googleapis/node-gtoken](https://github.com/googleapis/node-gtoken)\n\n## Installation\n\n``` sh\nnpm install gtoken\n```\n\n## Usage\n\n### Use with a `.pem` or `.json` key file:\n\n``` js\nconst { GoogleToken } = require('gtoken');\nconst gtoken = new GoogleToken({\n  keyFile: 'path/to/key.pem', // or path to .json key file\n  email: 'my_service_account_email@developer.gserviceaccount.com',\n  scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes\n  eagerRefreshThresholdMillis: 5 * 60 * 1000\n});\n\ngtoken.getToken((err, tokens) =\u003e {\n  if (err) {\n    console.log(err);\n    return;\n  }\n  console.log(tokens);\n  // {\n  //   access_token: 'very-secret-token',\n  //   expires_in: 3600,\n  //   token_type: 'Bearer'\n  // }\n});\n```\n\nYou can also use the async/await style API:\n\n``` js\nconst tokens = await gtoken.getToken()\nconsole.log(tokens);\n```\n\nOr use promises:\n\n```js\ngtoken.getToken()\n  .then(tokens =\u003e {\n    console.log(tokens)\n  })\n  .catch(console.error);\n```\n\n### Use with a service account `.json` key file:\n\n``` js\nconst { GoogleToken } = require('gtoken');\nconst gtoken = new GoogleToken({\n  keyFile: 'path/to/key.json',\n  scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes\n  eagerRefreshThresholdMillis: 5 * 60 * 1000\n});\n\ngtoken.getToken((err, tokens) =\u003e {\n  if (err) {\n    console.log(err);\n    return;\n  }\n  console.log(tokens);\n});\n```\n\n### Pass the private key as a string directly:\n\n``` js\nconst key = '-----BEGIN RSA PRIVATE KEY-----\\nXXXXXXXXXXX...';\nconst { GoogleToken } = require('gtoken');\nconst gtoken = new GoogleToken({\n  email: 'my_service_account_email@developer.gserviceaccount.com',\n  scope: ['https://scope1', 'https://scope2'], // or space-delimited string of scopes\n  key: key,\n  eagerRefreshThresholdMillis: 5 * 60 * 1000\n});\n```\n\n## Options\n\n\u003e Various options that can be set when creating initializing the `gtoken` object.\n\n- `options.email or options.iss`: The service account email address.\n- `options.scope`: An array of scope strings or space-delimited string of scopes.\n- `options.sub`: The email address of the user requesting delegated access.\n- `options.keyFile`: The filename of `.json` key or `.pem` key.\n- `options.key`: The raw RSA private key value, in place of using `options.keyFile`.\n- `options.additionalClaims`: Additional claims to include in the JWT when requesting a token.\n- `options.eagerRefreshThresholdMillis`: How long must a token be valid for in order to return it from the cache. Defaults to 0.\n\n### .getToken(callback)\n\n\u003e Returns the cached tokens or requests a new one and returns it.\n\n``` js\ngtoken.getToken((err, token) =\u003e {\n  console.log(err || token);\n  // gtoken.rawToken value is also set\n});\n```\n\n### .getCredentials('path/to/key.json')\n\n\u003e Given a keyfile, returns the key and (if available) the client email.\n\n```js\nconst creds = await gtoken.getCredentials('path/to/key.json');\n```\n\n### Properties\n\n\u003e Various properties set on the gtoken object after call to `.getToken()`.\n\n- `gtoken.idToken`: The OIDC token returned (if any).\n- `gtoken.accessToken`: The access token.\n- `gtoken.expiresAt`: The expiry date as milliseconds since 1970/01/01\n- `gtoken.key`: The raw key value.\n- `gtoken.rawToken`: Most recent raw token data received from Google.\n\n### .hasExpired()\n\n\u003e Returns true if the token has expired, or token does not exist.\n\n``` js\nconst tokens = await gtoken.getToken();\ngtoken.hasExpired(); // false\n```\n\n### .revokeToken()\n\n\u003e Revoke the token if set.\n\n``` js\nawait gtoken.revokeToken();\nconsole.log('Token revoked!');\n```\n\n## Downloading your private `.json` key from Google\n\n1. Open the [Google Developer Console][gdevconsole].\n2. Open your project and under \"APIs \u0026 auth\", click Credentials.\n3. Generate a new `.json` key and download it into your project.\n\n## Converting your `.p12` key to a `.pem` key\n\nIf you'd like to convert to a `.pem` for use later, use OpenSSL if you have it installed.\n\n``` sh\n$ openssl pkcs12 -in key.p12 -nodes -nocerts \u003e key.pem\n```\n\nDon't forget, the passphrase when converting these files is the string `'notasecret'`\n\n## License\n\n[MIT](https://github.com/googleapis/node-gtoken/blob/main/LICENSE)\n\n[codecov-image]: https://codecov.io/gh/googleapis/node-gtoken/branch/main/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/googleapis/node-gtoken\n[gdevconsole]: https://console.developers.google.com\n[gts-image]: https://img.shields.io/badge/code%20style-google-blueviolet.svg\n[gts-url]: https://www.npmjs.com/package/gts\n[npm-image]: https://img.shields.io/npm/v/gtoken.svg\n[npm-url]: https://npmjs.org/package/gtoken\n[snyk-image]: https://snyk.io/test/github/googleapis/node-gtoken/badge.svg\n[snyk-url]: https://snyk.io/test/github/googleapis/node-gtoken\n[client-docs]: https://googleapis.dev/nodejs/gtoken/latest/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogleapis%2Fnode-gtoken","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoogleapis%2Fnode-gtoken","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogleapis%2Fnode-gtoken/lists"}