Skip to content

Instantly share code, notes, and snippets.

@brianwmunz
Last active November 5, 2021 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianwmunz/5db6a6ddbabffa8df80ad080f2eeaff0 to your computer and use it in GitHub Desktop.
Save brianwmunz/5db6a6ddbabffa8df80ad080f2eeaff0 to your computer and use it in GitHub Desktop.
Authenticate your expert.ai NL API connection and set a variable with the bearer token in node.js / javascript
var uname = "username@faketest.com";
var pass = "fakePassword";
// Get a token from the NL API by creating a promise that retrieves the token when needed.
const token = new Promise((resolve, reject) => { //we're setting the variable 'token' to the value that is returned
var jsonBody = { "username": uname, "password": pass };
fetch("https://developer.expert.ai/oauth2/token", { //send the username and password to the auth endpoint
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8"
},
body: JSON.stringify(jsonBody) //get the response which contains the token text
})
.then((response) => response.text()) // the response from the API is in plain text
.then((res) => resolve(res)); // resolve the promise with the value of the token returned
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment