googleapis / googleapis/google-api-nodejs-client

Programmatically catch exceptions arising from authorization-related issues

Offen
#2,940 0 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

priority: p3 type: question
Vorherrschende Sprache
TypeScript
Sterne
12.3k
Forks
2k
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
24

Beschreibung

**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!

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
node.js, typescript
Bereich
api, authorization
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.