Skip to content

Instantly share code, notes, and snippets.

@wadewegner
Last active July 2, 2017 15:03
Show Gist options
  • Select an option

  • Save wadewegner/0a59a8995be5658a07a2051ccbce07e3 to your computer and use it in GitHub Desktop.

Select an option

Save wadewegner/0a59a8995be5658a07a2051ccbce07e3 to your computer and use it in GitHub Desktop.
Getting an Einstein (Metamind) auth token
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