Last active
July 2, 2017 15:03
-
-
Save wadewegner/0a59a8995be5658a07a2051ccbce07e3 to your computer and use it in GitHub Desktop.
Getting an Einstein (Metamind) auth token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const jwt = require('jsonwebtoken'); | |
| const request = require('request'); | |
| const url = 'https://api.metamind.io/'; | |
| const account_id = '<SNIP>'; | |
| const private_key = `-----BEGIN RSA PRIVATE KEY----- | |
| <SNIP> | |
| -----END RSA PRIVATE KEY----- | |
| `; | |
| const reqUrl = `${url}v1/oauth2/token`; | |
| // JWT payload | |
| const rsa_payload = { | |
| 'sub': account_id, | |
| 'aud': url | |
| }; | |
| const rsa_options = { | |
| header: { | |
| 'alg': 'RS256', | |
| 'typ': 'JWT' | |
| }, | |
| expiresIn: '1h' | |
| }; | |
| // Sign the JWT payload | |
| const assertion = jwt.sign( | |
| rsa_payload, | |
| private_key, | |
| rsa_options | |
| ); | |
| const options = { | |
| url: reqUrl, | |
| headers: { | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| 'accept': 'application/json' | |
| }, | |
| body: `grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=${encodeURIComponent(assertion)}` | |
| }; | |
| // Make the OAuth call to generate a token | |
| request.post(options, (error, response, body) => { | |
| // var data = JSON.parse(body); | |
| // console.log(data['access_token']); | |
| console.log(body); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment