I've been following the documentation on how to create an OAuth
token for Salesforce Einstein
. I have my private key
and able to create the JWT
token (code on bottom) yet when I try the CURL
command as outlined in the documentation (step 4) with the token I've retrieved I get INVALID JWT Assertion
error.
Documentation : https://metamind.readme.io/docs/generate-an-oauth-token-using-your-key
This is how I'm creating the signed JWT
require('dotenv').config();
const jwtToken = require('jsonwebtoken');
const payload =
{
"sub": "<EINSTEIN_PLATFORM_SERVICES_USERNAME>",
"aud": "https://api.einstein.ai/v2/oauth2/token",
"exp": <EXPIRATION_SECONDS_IN_UNIX_TIME>
}
const token = jwtToken.sign(payload, process.env.EINSTEIN_KEY, {algorithm : 'RS256'});
Any idea what I'm doing wrong?
It took me a while to figure this out...the problem was the UNIX
time I gave was something of the year 1970
-_- so of course, since the JWT
token expired I was not able to retrieve an access token from Salesforce Einstein
.
I suggest using the following resource to get the correct expiration time in UNIX
seconds : https://www.epochconverter.com/.
Here's my final code for generating a JWT Assertion String
:
require('dotenv').config()
const fs = require('fs);
const jwt = require('jsonwebtoken');
let privateKey = fs.readFileSync(process.env.PRIVATE_KEY);
const header = {
algorithm: "RS256"
}
const payload = {
"sub": "<EINSTEIN_PLATFORM_SERVICES_USERNAME>",
"aud": "https://api.einstein.ai/v2/oauth2/token",
"exp": 1907891585 //Epoch timestamp of 6/17/2030
}
let token = jwt.sign(payload, privateKey, header);
With this code, I was able to retrieve an access token!