I have been testing FX and scratch orgs with jsforce and I based my first tests with this example. https://jsforce.github.io/blog/posts/20170629-salesforce-dx-with-jsforce.html With the recommendation on the page, I tried using the JWT flow, but I am encountering auth issue with the created org. I just can't connect to it. I have followed https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_auth_connected_app.htm.
Am I missing something or there is an issue in DX beta?
Here is a snippet of code I put together to show the flow I do.
const jwt = require('jsonwebtoken');
const rp = require('request-promise');
let options = {
issuer: clientId,
audience: audience,
expiresIn: 20000,
algorithm: 'RS256'
};
let token = jwt.sign({prn: userName}, privateKey, options);
const accessToken = await rp(
{
method: 'POST',
uri: 'https://login.salesforce.com/services/oauth2/token',
form: {
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion': token
}
})
.then(response => {
return reply.access_token;
});
let loginUrl = "https://CS65.salesforce.com";
const hubConn = new jsforce.Connection(
{
instanceUrl: loginUrl,
accessToken: accessToken,
version: "40.0"
}
);
const scratchOrgInfo = hubConn.sobject('scratchOrgInfo');
await scratchOrgInfo.describe();
// create scratch org
console.log('Creating scratch org....');
const {id, success, errors} = await scratchOrgInfo.create({
Country: 'US',
Edition: 'Developer',
OrgName: 'testorg',
AdminEmail: AdminEmail,
ConnectedAppConsumerKey: clientId,
ConnectedAppCallbackUrl: redirectUrl,
});
if (!success) {
console.error(errors);
throw new Error('Error occurred while creating scratch org');
}
const orgInfo = await scratchOrgInfo.retrieve(id);
// establish connection with scratch org
const loginUrl = `https://${orgInfo.SignupInstance}.salesforce.com`;
const scratchOrgConn = new jsforce.Connection(
{
instanceUrl: loginUrl,
accessToken: accessToken,
version: "40.0"
}
);
const scratchOrgIdentity = await scratchOrgConn.identity() //Fails here on calling https://{instanceUrl}.salesforce.com/services/data/v40.0