0

I am trying to get access_token using authenticate method of nforce. I am doing everything right, but it still gives,

Error: invalid_grant - authentication failure.

Here's my code:

var nforce = require('nforce');
var port = 3333;
var org = nforce.createConnection({
  clientId: 'xxxxxxxxxx',
  clientSecret: 'xxxxxxxxx',
  redirectUri: 'http://localhost:3333/getAccessToken'
});


var express = require('express');
var app = express();
app.get('/getAccessToken', function(req,res) {
  console.log(req.query.code);
  org.authenticate({ code: req.query.code }, function(err, resp){
    console.log(err, resp);
  });
});

app.listen(port, function (err) {
  if (err) {
    return;
  }
  console.log('Server running on ', port);
});

And I've also tried same with the POST call to https://login.salesforce.com/services/oauth2/token, still same error.

I am thinking maybe it's some settings issue in my SF connected app.

Can anyone help me out here? Thanks!

closed as off-topic by Eric, Raul, battery.cord, Santanu Boral, Mohith Shrivastava May 11 '17 at 23:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to Salesforce are off-topic here, but can be asked on Stack Overflow." – Eric, Raul, battery.cord, Santanu Boral, Mohith Shrivastava
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Where are you posting to. Should end in authorize. Also you seem to be missing the response_type param – Eric May 11 '17 at 7:48
  • developer.salesforce.com/docs/atlas.en-us.api_rest.meta/…, in this doc, I have successfully completed 1st 3 steps but stuck at 4th one. And 4th one don't have response_type param – Mukul Jain May 11 '17 at 8:03
  • 1
    Then you are missing grant_type. Please update your question to be more clear and specific. It's hard to guess what you are doing and where you are stuck. How to Ask – Eric May 11 '17 at 8:06
1

Ok, I found the issue myself. After changing redirectUrl from 'https://successivesoftware.keka.com/#/me/attendance' to 'localhost', it worked.

Here's some sample code for nodejs using express and nforce.

var nforce = require('nforce');
var port = 3333;
var org = nforce.createConnection({
clientId: 'xxxxxxx',
clientSecret: 'xxxxxx',
redirectUri: 'http://localhost:3333/getAccessToken'
});

var express = require('express');
var app = express();
app.get('/getAccessToken', function(req,res) {
console.log(req.query.code);
org.authenticate({ code: req.query.code }, function(err, resp){
console.log(err, resp);
});
});

app.listen(port, function (err) {
if (err) {
return;
}
console.log('Server running on ', port);
});

Hope it helps other like me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.