如何在节点JS中使用Slack获取代码参数和oAuth进程?
How to get the code parameter in and oAuth process with Slack in node JS?
我正在尝试为 slack 开发一个应用程序。
我想通过松弛按钮获取代码参数发送以响应 oauth 流程,但我不知道如何获取参数。
事实上,我先发送了按钮,然后有人可以点击它并在其松弛通道上实施应用程序,然后 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/add_to_slack@2x.png 2x" /></a>');
console.log('cb0:le bouton slack s\'affiche');
router.get('/oauth/',recupCode);
};
router.get('/',boutonSlack);
app.listen(port, function () {
console.log('Ready');
});
你说你想获取代码 - 在用户点击后,访问代码作为 url 参数从 Slack 在 GET 请求中发送到你的应用程序在您的 添加到 Slack 按钮上并授权 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
该按钮通常显示在网站上,节点应用程序充当服务器,等待来自 Slack 的授权请求。
https://api.slack.com/docs/slack-button
这就是我在节点应用程序中设置 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
}
});
我正在尝试为 slack 开发一个应用程序。 我想通过松弛按钮获取代码参数发送以响应 oauth 流程,但我不知道如何获取参数。
事实上,我先发送了按钮,然后有人可以点击它并在其松弛通道上实施应用程序,然后 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/add_to_slack@2x.png 2x" /></a>');
console.log('cb0:le bouton slack s\'affiche');
router.get('/oauth/',recupCode);
};
router.get('/',boutonSlack);
app.listen(port, function () {
console.log('Ready');
});
你说你想获取代码 - 在用户点击后,访问代码作为 url 参数从 Slack 在 GET 请求中发送到你的应用程序在您的 添加到 Slack 按钮上并授权 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
该按钮通常显示在网站上,节点应用程序充当服务器,等待来自 Slack 的授权请求。
https://api.slack.com/docs/slack-button
这就是我在节点应用程序中设置 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
}
});