Skip to content

Commit

Permalink
Update OAuth docs
Browse files Browse the repository at this point in the history
  • Loading branch information
LFDM committed Dec 26, 2020
1 parent 3d2083e commit c1d69d1
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions docs/getting-started/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ You have several options for how you want to connect, but most projects should u

- [Service Account](#service-account) - connects as a specific "bot" user generated by google for your application
- [API-key](#api-key) - only identifies your application, provides read-only access
- [OAuth](#oauth) - connect on behalf of a specific user (coming soon!)
- [OAuth](#oauth) - connect on behalf of a specific user

**👉 BUT FIRST -- Set up your google project & enable the sheets API 👈**
1. Go to the [Google Developers Console](https://console.developers.google.com/)
Expand Down Expand Up @@ -98,4 +98,32 @@ doc.useApiKey(process.env.GOOGLE_API_KEY);
## 👨‍💻 OAuth 2.0 :id=oauth
**connect on behalf of a user with an Oauth token**

!> Support and instructions coming soon
Use [Google's OAuth2Client](https://github.com/googleapis/google-auth-library-nodejs#oauth2) to authenticate.

```javascript
const = oAuth2Client = new OAuth2Client({
clientId: process.env.GOOGLE_OAUTH_CLIENT_ID,
clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET
})

// Pre-configure the client with credentials you have stored in e.g. your databse
// At a minimum, provide only the refresh_token. The client will use it to retrieve
// a fresh access_token, with a fresh expiration date.
oAuth2Client.credentials.access_token = accessToken;
oAuth2Client.credentials.refresh_token = refreshToken;
oAuth2Client.credentials.expiry_date = expiryDate; // Unix epoch milliseconds


// Listen in whenever a new access token is obtained. You might want to store them in your database.
// Mind that the refresh_token never changes (unless it's revoked, in which case your end-user will
// need to go through the full authentication flow again), so storing the new access_token is optional.
oAuth2Client.on('tokens', credentials => {
console.log(credentials.access_token);
console.log(credentials.scope);
console.log(credentials.expiry_date);
console.log(credentials.token_type); // will always be 'Bearer'
})

const doc = new GoogleSpreadsheet('<YOUR-DOC-ID>');
doc.useOAuth2Client(oauth2Client);
```

0 comments on commit c1d69d1

Please sign in to comment.