jsforce
jsforce copied to clipboard
Automated accessToken refresh oAuth2 not working
Hi there,
I'm trying to connect jsforce with the oAuth2, which is working as expected. However i'm running into an issue with the automated refresh from the AccessToken.
I'm using the following code;
const conn = new jsforce.Connection({
oauth2: {
clientId: process.env.VUE_APP_SALESFORCE_CLIENT_ID,
clientSecret: process.env.VUE_APP_SALESFORCE_CLIENT_SECRET,
loginUrl: process.env.VUE_APP_SALESFORCE_LOGIN_URL,
redirectUri: 'http://localhost:8080/salesforce/oauth/callback',
},
accessToken: accessToken || process.env.VUE_APP_SALESFORCE_ACCESS_TOKEN,
refreshToken: process.env.VUE_APP_SALESFORCE_REFRESH_TOKEN,
instanceUrl: process.env.VUE_APP_SALESFORCE_INSTANCE_URL,
version: '53.0',
logLevel: 'DEBUG',
})
conn.sobject('Case').create(record, (err, ret) => {
if (err) {
console.error(err)
return
}
console.log(`[created case] - id: ${ret.id}`)
}
This works fine in the beginning, however once my accessToken is expired then i expect the jsforce service to refresh the token automatically. However i experience the following issue:
Status Code: 401 which can been seen in my network tab. However once i check the debug logs from the jsforce sobject creation it doesn't recognize as a 401 statusCode but a 400 statusCode.
<response> status=400, url=https://my-instance-url-was-here/services/data/v53.0/sobjects/Case
I did a little bit of investigation and found out that in the following file;
node_modules/jsforce/lib/browser/request.js:50
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var headerNames = getResponseHeaderNames(xhr);
var headers = {};
_.forEach(headerNames, function(headerName) {
if (headerName) {
headers[headerName] = xhr.getResponseHeader(headerName);
}
});
response = {
statusCode: xhr.status,
headers: headers,
body: xhr.response
};
if (!response.statusCode) {
response.statusCode = 400;
response.body = "Access Declined";
}
if (callback) {
callback(null, response, response.body);
}
str.end();
}
};
I've added some debugging to check if the xhr
request is having any information, but that seems like an empty request.
XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …}
onabort: null
onerror: null
onload: null
onloadend: null
onloadstart: null
onprogress: null
onreadystatechange: ƒ ()
ontimeout: null
readyState: 4
response: ""
responseText: ""
responseType: ""
responseURL: ""
responseXML: null
status: 0
statusText: ""
timeout: 0
upload: XMLHttpRequestUpload {onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, …}
withCredentials: false
[[Prototype]]: XMLHttpRequest
Right now i have a workaround in place where i just refresh the token as soon as my case returns an 400 error ( the default statusCode while the XHR request is empty ). But the package does mention it should refresh automatically.
- https://jsforce.github.io/document/#access-token-with-refresh-token