重定向网址后无法获取授权代码

时间:2020-04-27 14:15:04

标签: node.js express

我正在从事Salesforce和Slack集成。而且我对javascript及其相关技术了解不多。您能调查一下代码,让我知道丢失了什么吗?

// 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);
}

1 个答案:

答案 0 :(得分:1)

在我看来,您正在将授权请求重定向到Salesforce,并要求Salesforce.com(SFDC)将其重定向回****/oauth(从查询参数redirect_uri=到SFDC URL

您是否希望它将重定向到您自己的/oauth端点?

如果是这样,SFDC可能会使用GET操作而不是POST操作来重定向它。请注意,GET的参数显示在req.params中,而不是req.body中。

尝试实现get()处理程序,以查看是否可行。

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