0

I have to call an Apex REST API I created in salesforce from my node application. I have created the connected app in salesforce and required express and jsforce in my node application.

But, I'm confused with the callback URLs, if I want to test on localhost(let's say port 3000) what should I specify for the Callback URL in the connected app? Also, with the JSForce oAuth2 method(https://jsforce.github.io/document/#oauth2)

var jsforce = require('jsforce');

var oauth2 = new jsforce.OAuth2({
    // you can change loginUrl to connect to sandbox or prerelease env.
    // loginUrl : 'https://test.salesforce.com',
    clientId : '<your Salesforce OAuth2 client ID is here>',
    clientSecret : '<your Salesforce OAuth2 client secret is here>',
    redirectUri : '<callback URI is here>'
});

what should the redirectUri be? Is it the same as the Callback URL in the connected app? Is the loginUrl required if I'm using oAuth2 method?

Does this also affect the express routes

app.get('/oauth2/auth', function(req, res) {
    res.redirect(oauth2.getAuthorizationUrl({ scope : 'api id web' }));
});

AND

app.get('/oauth2/callback', function(req, res) {
    var conn = new jsforce.Connection({ oauth2 : oauth2 });
    var code = req.param('code');
    conn.authorize(code, function(err, userInfo) {
        // ...
    });
});

If you can provide sample code, that'd be great.

Any help is greatly appreciated, thanks in advance!

1
  • Can someone provide ANY method (sample code) to call Salesforce Apex REST API from node js. Please and thank you! Dec 18, 2018 at 16:09

1 Answer 1

2

if I want to test on localhost(let's say port 3000) what should I specify for the Callback URL in the connected app?

Callback URL is always where you want the access token code to be send to after successful authorization. So if you want it to be sent to a localhost on a specific port, then Yes, you will need to provide that URL in connected app.

what should the redirectUri be? Is it the same as the Callback URL in the connected app?

Yes. redirectUri is the location that is provided where the access token is sent. And it's the same that you have configured in the callback URL in your connected app.

This is from the documentation:

redirect_uri is the Callback URL.

About loginUrl:

Is the loginUrl required if I'm using oAuth2 method?

I am not quite sure about JSforce, but seems like it uses the loginUrl to create the URLs for getting token, e.g., loginUrl/services/oauth2/token.

This site is temporarily in read-only mode and not accepting new answers.

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