After authenticating with an org via sfdx's (which uses oauth under the hood), it stores the accessToken and other details in a json file in the ~/.sfdx folder.
Example:
{ orgId: '00Dd0000000cddqEAA',
accessToken: 'aaa7d0647d....3f427',
refreshToken: '9ca044f...99444886',
instanceUrl: 'https://org.my.salesforce.com',
loginUrl: 'https://login.salesforce.com',
username: '[email protected]',
isDevHub: true }
If I use the accessToken and instanceUrl from that file:
const conn = new jsforce.Connection({instanceUrl, accessToken});
I get this error:
sf:INVALID_SESSION_ID: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session
Any ideas?
I just tried this as well. I believe it's because the accessToken that's stored is not the real one. It's encrypted for security. I tested this by getting org:display from the cli and comparing the two.
Rather than parse the ~/.sfdx folder, run theforce:org:display command with json output to obtain the access token.
sfdx force:org:display -u myorg --json
{
"status":0,
"result":{
"password":"foo123",
"username":"[email protected]",
"devHubId":"[email protected]",
"id":"00DR0000001oKnrMBE",
"createdBy":"[email protected]",
"createdDate":"2018-05-08T02:12:36.000+0000",
"expirationDate":"2018-05-15",
"status":"Active",
"edition":"Developer",
"orgName":"Company",
"accessToken":"00DR...6unNVNRBWcP2i",
"instanceUrl":"https://<instance>.my.salesforce.com",
"clientId":"SalesforceDevelopmentExperience",
"alias":"myorg"
}
}
@DouglasCAyers Awesome! Thanks.
@douglascayers @JonDum have you run into invalid grant issues? if so how have you gotten around them?
My guess is that the force:org:display command is not refreshing the token before showing it, perhaps displaying the last obtained value which may or may not still be valid. Or, maybe the scratch org has expired and the token is literally no longer valid.
You might try running the force:limits:api:display command first which likely will require the access token to be refreshed because the CLI has to make a REST API call and would auto-renew the token if necessary.
@douglascayers will that also refresh the access token for scratch orgs?
@JonDum @salesforce/core library (sfdx-cli depends on this library) extends objects from jsforce.
You can use the following snippet to get jsforce Connnection object:
const {AuthInfo, Connection} = require('@salesforce/core');
const conn = await Connection.create({
authInfo: await AuthInfo.create({ username: 'myAdminUsername' })
});
https://forcedotcom.github.io/sfdx-core/classes/connection.html
Most helpful comment
Rather than parse the
~/.sfdxfolder, run theforce:org:displaycommand with json output to obtain the access token.