### Install client-oauth2 Source: https://github.com/n8n-io/js-client-oauth2-extended/blob/master/README.md Install the client-oauth2 library using npm. ```sh npm install client-oauth2 --save ``` -------------------------------- ### Implicit Grant: Get Token and Make Request Source: https://github.com/n8n-io/js-client-oauth2-extended/blob/master/README.md Use this for obtaining access tokens in browser-based applications. Redirect the user to get an authorization URI, then parse the response URI to get the token and make authenticated requests. ```javascript window.oauth2Callback = function (uri) { githubAuth.token.getToken(uri) .then(function (user) { console.log(user) //=> { accessToken: '...', tokenType: 'bearer', ... } // Make a request to the github API for the current user. return popsicle.request(user.sign({ method: 'get', url: 'https://api.github.com/user' })).then(function (res) { console.log(res) //=> { body: { ... }, status: 200, headers: { ... } } }) }) } // Open the page in a new window, then redirect back to a page that calls our global `oauth2Callback` function. window.open(githubAuth.token.getUri()) ``` -------------------------------- ### Client Credentials Grant: Get Token Source: https://github.com/n8n-io/js-client-oauth2-extended/blob/master/README.md Use this when the client needs access to protected resources under its control. Get the access token for the application using client credentials. ```javascript githubAuth.credentials.getToken() .then(function (user) { console.log(user) //=> { accessToken: '...', tokenType: 'bearer', ... } }) ``` -------------------------------- ### JWT Bearer Grant: Get Token Source: https://github.com/n8n-io/js-client-oauth2-extended/blob/master/README.md Obtain an access token by utilizing an existing trust relationship expressed through a JWT. Provide the JWT to the `getToken` method. ```javascript githubAuth.jwt.getToken('eyJhbGciOiJFUzI1NiJ9.eyJpc3Mi[...omitted for brevity...].J9l-ZhwP[...omitted for brevity...]') .then(function (user) { console.log(user) //=> { accessToken: '...', tokenType: 'bearer', ... } }) ``` -------------------------------- ### Authorization Code Grant Flow - Get Token Source: https://github.com/n8n-io/js-client-oauth2-extended/blob/master/README.md Handle the callback from the authorization server, exchange the authorization code for an access token, and manage the user's session. ```javascript app.get('/auth/github/callback', function (req, res) { githubAuth.code.getToken(req.originalUrl) .then(function (user) { console.log(user) //=> { accessToken: '...', tokenType: 'bearer', ... } // Refresh the current users access token. user.refresh().then(function (updatedUser) { console.log(updatedUser !== user) //=> true console.log(updatedUser.accessToken) }) // Sign API requests on behalf of the current user. user.sign({ method: 'get', url: 'http://example.com' }) // We should store the token into a database. return res.send(user.accessToken) }) }) ``` -------------------------------- ### Resource Owner Password Credentials Grant: Get Token Source: https://github.com/n8n-io/js-client-oauth2-extended/blob/master/README.md Suitable for trusted clients like operating system applications. Make a direct request for the access token using the username and password. ```javascript githubAuth.owner.getToken('blakeembrey', 'hunter2') .then(function (user) { console.log(user) //=> { accessToken: '...', tokenType: 'bearer', ... } }) ``` -------------------------------- ### Initialize GitHub OAuth Client Source: https://github.com/n8n-io/js-client-oauth2-extended/blob/master/README.md Create a new instance of the OAuth 2.0 client for GitHub authentication. ```javascript var ClientOAuth2 = require('client-oauth2') var githubAuth = new ClientOAuth2({ clientId: 'abc', clientSecret: '123', accessTokenUri: 'https://github.com/login/oauth/access_token', authorizationUri: 'https://github.com/login/oauth/authorize', redirectUri: 'http://example.com/auth/github/callback', scopes: ['notifications', 'gist'] }) ``` -------------------------------- ### Create and Manage Access Token Source: https://github.com/n8n-io/js-client-oauth2-extended/blob/master/README.md Create a token instance, set its expiration, and refresh it. This is useful for managing user credentials after initial authentication. ```javascript // Can also just pass the raw `data` object in place of an argument. var token = githubAuth.createToken('access token', 'optional refresh token', 'optional token type', { data: 'raw user data' }) // Set the token TTL. token.expiresIn(1234) // Seconds. token.expiresIn(new Date('2016-11-08')) // Date. // Refresh the users credentials and save the new access token and info. token.refresh().then(storeNewToken) // Sign a standard HTTP request object, updating the URL with the access token // or adding authorization headers, depending on token type. token.sign({ method: 'get', url: 'https://api.github.com/users' }) //=> { method, url, headers, ... } ``` -------------------------------- ### Authorization Code Grant Flow - Redirect Source: https://github.com/n8n-io/js-client-oauth2-extended/blob/master/README.md Initiate the Authorization Code Grant flow by redirecting the user to the authorization server. ```javascript var express = require('express') var app = express() app.get('/auth/github', function (req, res) { var uri = githubAuth.code.getUri() res.redirect(uri) }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.