theoephraim / node-google-spreadsheet

Google Sheets API wrapper for Javascript / Typescript
https://theoephraim.github.io/node-google-spreadsheet
The Unlicense
2.3k stars 388 forks source link

Error: Request to googleapis.com/oauth2/v4/token Failed When Using Google Spreadsheet npm Package with VPN in China (Works Fine in Morocco) #684

Open Adoo2401 opened 5 months ago

Adoo2401 commented 5 months ago

I'm currently working on a project where I need to access Google Sheets using the google-spreadsheet npm package. Due to restrictions, I'm based in China and using a VPN to access Google services. However, I'm encountering an issue where I'm getting the following error Error: Request to https://www.googleapis.com/oauth2/v4/token failed, reason:

Here's the relevant code snippet: `import { JWT } from "google-auth-library"; import { GoogleSpreadsheet} from "google-spreadsheet";

const serviceAccountAuth = new JWT({ email: creds.client_email, key: creds.private_key, scopes: SCOPES, });

const doc = new GoogleSpreadsheet(googleSheetId, serviceAccountAuth);`

I suspect this error is due to the VPN and possibly related to Google's restrictions in China. How can I resolve this issue and successfully authenticate with Google's OAuth2 service while using a VPN in China? Any insights or workaround suggestions would be greatly appreciated. Thank you!

Note: I've tested this code with a friend located in Morocco, and it works fine on their laptop without any issues.

theoephraim commented 5 months ago

Sorry but that’s really outside the scope of this project…

But it sounds like you need to route the google requests through a proxy. This is very common in scraping applications so just search for things related to scraping, proxies, and changing countries.

Internally this tool uses axios, so I think after initializing the document, you should be able to access the axios instance and mess with the settings to inject proxy behaviour.