sumito
sumito

Reputation: 23

Can't fetch the authorization code after the redirect url

I am working on Salesforce and Slack integration. And I don't know much about javascript and its related technologies. Could you please look into the code and let me know whats missing?

// Import express and request moduless
var express = require('express');
var request = require('request');
var url = require('url');
var clientId = '****';
var clientSecret = '****';
var SF_LOGIN_URL = "http://login.salesforce.com";
var SF_CLIENT_ID = "****";
// We define the port we want to listen to. Logically this has to be the same port than we specified on ngrok.
const PORT=4390;

// Instantiates Express and assigns our app variable to it
var app = express();
app.enable('trust proxy');

//var server = http.createServer(app);
//Lets start our server
app.listen(PORT, function () {
    //Callback triggered when server is successfully listening.
    console.log("Example app listening on port " + PORT);
});

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

// Route the endpoint that our slash command will point to and send back a simple response to indicate that ngrok is working
app.post('/oauth', function(req, res) {
    oauth(req, res);
});

function oauth(req, res){
    res.redirect(200, `${SF_LOGIN_URL}/services/oauth2/authorize?response_type=code&client_id=${SF_CLIENT_ID}&redirect_uri=****/oauth&display=touch}`);
    console.log(url.location.href);
}

Upvotes: 1

Views: 370

Answers (1)

O. Jones
O. Jones

Reputation: 108816

It looks to me like you're redirecting an authorization request to Salesforce, and asking Salesforce.com (SFDC) to redirect it back to ****/oauth (from the redirect_uri= query parameter to the SFDC URL.

Are you hoping it will get redirected back to your own /oauth endpoint?

If so, it's possible SFDC is redirecting it with a GET operation rather than a POST operation. Be aware that the parameters to a GET show up in req.params rather than req.body.

Try implementing a get() handler to see if you get something workable.

app.get('/oauth', function(req, res) {
  console.log ('GET /oauth', req.params)
  /* do something here */
});

Upvotes: 1

Related Questions