我正在尝试开发一个 Slack 应用程序。 我想使用 slack 按钮获取代码参数发送以回答 oauth 流程,但我不知道如何获取参数。
事实上,我首先发送了按钮,然后有人可以单击它并在其 Slack channel 上实现应用程序,然后 oauth 流程将我重定向到一个新网页,其 url 是 https://www.myappname.com/oauth/?code=[parameter我不想得到]&state=
问题是我获取代码参数的方法不等待重定向。
这是我的代码:
var app = express();
var router = express.Router();
var port = process.env.PORT || 5000;
app.use('/', router);
recupCode = function(req, res, next){
console.log(req.params);
console.log('cb1 : le code est récupéré');
res.end();
};
//Fonctions de Callback
boutonSlack = function(req, res) {
res.send('<a href="https://slack.com/oauth/authorize?scope=incoming-webhook,'
+'&client_id='+process.env.CLIENT_ID+'">'
+'<img alt="Add to Slack" height="40" width="139"'
+'src="https://platform.slack-edge.com/img/add_to_slack.png" '
+'srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, '
+'https://platform.slack-edge.com/img/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a1a4a49fb4af9fb3aca1a3ab80f2b8eeb0aea7" rel="noreferrer noopener nofollow">[email protected]</a> 2x" /></a>');
console.log('cb0:le bouton slack s\'affiche');
router.get('/oauth/',recupCode);
};
router.get('/',boutonSlack);
app.listen(port, function () {
console.log('Ready');
});
最佳答案
您说过您想要获取代码 - 在用户点击“添加到 Slack”后,访问代码将作为 url 参数从 Slack 通过 GET 请求发送到您的应用/em> 按钮并授权 Slack 安装您的应用程序的请求。您的应用在 router.get('/', function(request, response){};
中等待来自 Slack 的这些请求,并且您使用 request.url 访问包含代码的字符串。
通过一些字符串操作,您可以从 URL 中提取 code 值,并在请求中调用 Slack 的 auth.access(client_id, client_secret, code)
来交换代码为您客户的 access_token。此 access_token 是您在团队中执行所有操作所用的,因此您需要存储它。
https://api.slack.com/methods/oauth.access
该按钮通常显示在网站上, Node 应用程序充当服务器,等待来自 Slack 的授权请求。
https://api.slack.com/docs/slack-button
这就是我在 Node 应用程序中设置 index.js 文件以等待安装请求的方式。我不直接使用路由器,我更喜欢请求库
const express = require('express');
const request = require('request'); //I prefer the request library to make requests
var path_to_access_token = "https://slack.com/api/oauth.access?client_id=[INSERT_CLIENT_ID]&client_secret=[INSERT_CLIENT_SECRET]&code="; //Slack URL to call to receive accessToken
var app = express();
/* WAIT FOR NEW APP INSTALLATION REQUESTS FROM SLACK */
app.get('/*', function(req, res) {
// Tease out accessCode from the Slack request, if it exists
var url = req.url;
var codePos = url.indexOf("code="); //index where code= starts in url
var codeStartPos = codePos + 5; //Start of accessCode (+5 because code= is 5 characters)
var endingPos = url.indexOf("&"); //End of accessCode, where another parameter starts
var accessCode = url.substring(codeStartPos, endingPos).toString(); //Extract code from url
// Verify user accepted Slack's auth request by looking for access_code existence
if (codePos > -1) { // User authorized oAuth request from Slack
var completePath = path + accessCode; //Slack API call + code to receive accessToken and teamInfo
request(completePath, function(error, response, body) { // Request token from Slack using the access_code, then handle response
if(!error && response.statusCode == 200 && teamInfo.ok == true){
var teamInfo = JSON.parse(body); //Slack sends back access_code and team info in a JSON object
//SAVE THE ACCESS_CODE
} else {
//ERROR
}
});
} else { //User denied auth request from Slack, so reroute back to signup page to start over
//REROUTE USER BACK TO INSTALL PAGE, THEY DENIED AUTH REQUEST
}
});
关于javascript - 如何在Node JS中使用Slack获取oAuth流程中的code参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999176/