node-google-spreadsheet
node-google-spreadsheet copied to clipboard
New auth mode
Edit : I created a pull request
Hi! 👋
Firstly, thanks for your work on this project! 🙂 It really is much simpler to use than the official API !
Today I used patch-package to patch [email protected]
for the project I'm working on.
I am deploying my code to a Google Cloud Function and none of the existing auth methods work easily in that context. So I added an AUTH_CLIENT auth mode that is super simple to use (no params required). It works when the code is run on GCP, and to run locally, you can just set a GOOGLE_APPLICATION_CREDENTIALS
env var to point to your creds file :-)
It could probably be made the default mode, since no configuration is required.
Here is the diff that solved my problem:
diff --git a/node_modules/google-spreadsheet/lib/GoogleSpreadsheet.js b/node_modules/google-spreadsheet/lib/GoogleSpreadsheet.js
index 98d3a19..7611e10 100644
--- a/node_modules/google-spreadsheet/lib/GoogleSpreadsheet.js
+++ b/node_modules/google-spreadsheet/lib/GoogleSpreadsheet.js
@@ -1,5 +1,5 @@
const _ = require('lodash');
-const { JWT } = require('google-auth-library');
+const { JWT, GoogleAuth} = require('google-auth-library');
const Axios = require('axios');
const GoogleSpreadsheetWorksheet = require('./GoogleSpreadsheetWorksheet');
@@ -21,6 +21,7 @@ const AUTH_MODES = {
API_KEY: 'API_KEY',
RAW_ACCESS_TOKEN: 'RAW_ACCESS_TOKEN',
OAUTH: 'OAUTH',
+ AUTH_CLIENT: 'AUTH_CLIENT'
};
class GoogleSpreadsheet {
@@ -78,6 +79,13 @@ class GoogleSpreadsheet {
}
// AUTH RELATED FUNCTIONS ////////////////////////////////////////////////////////////////////////
+ async useDefaultAuthClient() {
+ this.authMode = AUTH_MODES.AUTH_CLIENT;
+ this.authClient = await new GoogleAuth({
+ scopes: GOOGLE_AUTH_SCOPES
+ }).getClient();
+ }
+
async useApiKey(key) {
this.authMode = AUTH_MODES.API_KEY;
this.apiKey = key;
@@ -127,7 +135,9 @@ class GoogleSpreadsheet {
// INTERNAL UTILITY FUNCTIONS ////////////////////////////////////////////////////////////////////
async _setAxiosRequestAuth(config) {
// TODO: check auth mode, if valid, renew if expired, etc
- if (this.authMode === AUTH_MODES.JWT) {
+ if (this.authMode === AUTH_MODES.AUTH_CLIENT) {
+ Object.assign(config.headers, await this.authClient.getRequestHeaders());
+ } else if (this.authMode === AUTH_MODES.JWT) {
if (!this.jwtClient) throw new Error('JWT auth is not set up properly');
// this seems to do the right thing and only renew the token if expired
await this.jwtClient.authorize();
This issue body was partially generated by patch-package.
Nice! This has been on my todo list for quite some time, but I didnt realize the google auth library would make it so simple. I'll try to get this merged in soon.
@theoephraim is this still planned to be merged in? I also want to use the library in a cloud function and I'd like to avoid including the json for credentials in the deploy.
Any updates on this? Thanks
ADC is now supported. Sorry it took so long. See https://theoephraim.github.io/node-google-spreadsheet/#/guides/authentication?id=adc