googleapis / googleapis/google-api-nodejs-client

Programmatically catch exceptions arising from authorization-related issues

Open
#2,940 0 comments 2 reactions 0 assignees View on GitHub
priority: p3 type: question
Dominant language
TypeScript
Stars
12.2k
Forks
2k
Avg merge
1d 9h
Merged PRs (30d)
24

Description

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.