googleapis / googleapis/google-api-nodejs-client

Programmatically catch exceptions arising from authorization-related issues

Aperta
#2,940 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub
priority: p3 type: question
Lingua principale
TypeScript
Stelle
12.2k
Fork
2k
Merge medio
1g 9h
PR unite (30g)
24

Descrizione

**Environment:**
- googleapis: "^94.0.0",
- node: v15.6.0

- What you're trying to do
Hi, I'm trying to programmatically catch exceptions that may arise from an authorization failure (e.g. if the clientId, clientSecret or even access/refresh tokens are invalid when their associated methods are called). My flow is to receive the credentials and tokens set them onto the oAuth2Client object, and then later on in the flow, perform a library call to create a file on the client's drive.
I'm looking to be able to detect if the credentials are invalid, catch the exception and elegantly handle it without crashing my server, much like what seems to be described in here: https://developers.google.com/maps/documentation/javascript/events#auth-errors.

- What code you've already tried
```
const { google } = require('googleapis');
google.options({
retryConfig: {
onRetryAttempt: (err) => {
console.log('err', err)
}
}
});

try {
...

...
const oAuth2Client: OAuth2Client = new google.auth.OAuth2(clientId, clientSecret, 'urn:ietf:wg:oauth:2.0:oob');
const credentialResponse = oAuth2Client.setCredentials(tokens); //the tokens are purposefully changed to be wrong here
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
const fileMetadata = {
name: ,
driveId: ,
parents: []
};
var media = {
mimeType: 'application/vnd.google-apps.document',
body: uploadStream //abstracted
};
const file = drive.files.create(
{
resource: fileMetadata,
media,
supportsAllDrives: true,
fields: 'id, name, webViewLink, mimeType, fileExtension, webContentLink'
},
{
onUploadProgress: <...>
}
}
} catch (err) {
console.error(`Upload failed: ${err}`);
}
```
- Any error messages you're getting
```
GaxiosError: invalid_client
at Gaxios._request (/Users/user/Desktop/CloudFileStorage/node_modules/gaxios/build/src/gaxios.js:133:23)
at processTicksAndRejections (node:internal/process/task_queues:94:5)
at async OAuth2Client.refreshTokenNoCache (/Users/user/Desktop/CloudFileStorage/node_modules/google-auth-library/build/src/auth/oauth2client.js:174:21)
at async OAuth2Client.getRequestMetadataAsync (/Users/user/Desktop/CloudFileStorage/node_modules/google-auth-library/build/src/auth/oauth2client.js:284:17)
at async OAuth2Client.requestAsync (/Users/user/Desktop/CloudFileStorage/node_modules/google-auth-library/build/src/auth/oauth2client.js:357:23) {
```
I'm unable to catch this gaxios error even when trying to break out the execution during the retryConfig or wrapping the calls in try catches. Thanks for taking the time to read this!

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start with the drive.files.create call and the OAuth2Client.refreshTokenNoCache, getRequestMetadataAsync, and requestAsync entries shown in the stack trace. Trace how the asynchronous authorization failure is surfaced and compare it with the supplied try/catch and retryConfig usage. Done means invalid credentials produce a reliably catchable failure without crashing the server.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
node.js, typescript
Ambito
api, authorization
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.