2

Premise: I need to integrate SF with NodeJs using JWT. I'm following Salesforce Developers Session for this purpose. As of now the entire setup of NodeJs is only in local. According to the video I have used the below code to achieve the integration.

const fs = require('fs');
const compression = require("compression");
const express = require("express");
const helmet = require("helmet");
const jsforce = require("jsforce");
const { getToken } = require("sf-jwt-token");
const dotenv = require('dotenv');

const privateKey = fs.readFileSync('private.pem').toString('utf8')

const result = dotenv.config();
if (result.error) {
    throw result.error
}
console.log('Hi');
console.log(result.parsed)

const HOST =  process.env.HOST;
const PORT = process.env.PORT;

const app = express();
app.use(helmet());
app.use(compression());

const conn = new jsforce.Connection();

app.get('/', async (req, res) => {
    try {
        const jwttokenresponse = await getToken({
            iss: process.env.CLIENT_ID,
            sub: process.env.USERNAME,
            aud: process.env.LOGIN_URL,
            privateKey: privateKey
        });
        
        console.log(jwttokenresponse.instance_url);
        console.log(jwttokenresponse.access_token);

        conn.initialize({
            instanceURL: jwttokenresponse.instance_url,
            accessToken: jwttokenresponse.access_token
        });
        
        const accList = await conn.query("Select Id, Name from Account LIMIT 20");
        res.json(accList);
    } catch (e) {
        console.log(e);
        res.json(JSON.stringify(e));
    }
});

app.listen(PORT, () => {
    console.log(`Server started on ${HOST} on port ${PORT}`);
});

==> Here, I was able to retrieve access_token perfectly fine.

Problem: When I'm trying to do conn.query I'm getting error as Error: Invalid URI "/services/data/v42.0/query?q=Select%20Id%2C%20Name%20from%20Account%20LIMIT%2020"

A few thing I'm not able to understand are

  1. When I have already received access_token, and have intialized it as well(using conn.initialize), is there anything else I have to do before making call for query?
  2. Why this error, and how to resolve this?
  3. I have not set the version as 42 anywhere , but then how in the error I'm getting version 42 of SF?

Note:

  1. When I tried same thing using credential based authentication I received the result.
  2. Selected OAuth Scopes in connected app is Full access (full)
  3. At connected App level, Permitted Users Admin approved users are pre-authorized has entire 'System Administrator' as of now.
1
  • 1
    Javascript is case sensitive, in the initialize call you are passing instanceURL, which should be instanceUrl.
    – H Alves
    Commented Dec 14, 2021 at 13:13

1 Answer 1

2

I am doing something similar but I am doing this instead of using the initialize method:

const loginRes = await sfApi.loginToSf(); //JWT Login

    conn = new jsforce.Connection({
        version:'50.0',
        instanceUrl: loginRes.instance_url,
        accessToken: loginRes.access_token
    });
7
  • Thanks, @Bryan, this seems to be working. If possible, can you please explain, why this was the behaviour, is it some bug at the jsforce level or this is the actual way the code should always be.
    – user43598
    Commented Dec 14, 2021 at 13:52
  • 1
    I imagine it has to do with the implementation of the initialization method. Other than that I don't know Commented Dec 14, 2021 at 14:31
  • 2
    @user43598 As I commented on the question, it's because Bryan uses instanceUrl instead of instanceURL
    – H Alves
    Commented Dec 14, 2021 at 15:17
  • Nice catch @HAlves Commented Dec 14, 2021 at 15:23
  • Thanks @H Alves, :)
    – user43598
    Commented Dec 14, 2021 at 19:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .