I am trying to authorize a request to the Salesforce API through oauth 2.0 with a JSON Web Token. I've managed to do this wit cURL in PHP, and the request works with Postman. However, when I need to do it with axios in my javascript, I always encounter CORS issues.
Here is my code:
var credentials = require('../credentials');
var jwtParams = {
iss: credentials.salesforce.consumer_key,
prn: credentials.salesforce.username,
aud: credentials.salesforce.url,
exp: Math.floor(new Date().getTime() / 1000 + 120)
};
var token = jwt.sign(jwtParams, this.state.privateKey, {
algorithm: 'RS256'
});
var params = {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: token
};
var params_string = querystring.stringify(params);
axios({
method: 'post',
url: 'https://login.salesforce.com/services/oauth2/token',
data: params_string,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(res) {
var conn = new jsforce.Connection({
instanceUrl: res.data.instance_url,
accessToken: res.data.access_token
});
});
So, essentially, I use the jsonwebtoken to create and sign a JWT (which does validate correctly on jwt.io with the certificate I attached to my Connected App). I then try connecting to Salesforce with that token, and that is where the CORS error happens.
Can anyone help with this?
Hi System Admin,
Sorry for the inconvenience. In case you don't receive a response here, may I also suggest joining the Developer forums below to collaborate with the experts for best practice and advice.
https://developer.salesforce.com/forums
https://salesforce.stackexchange.com/
Hope that helps.
Regards,
Jayson