gpt4 book ai didi

javascript - 如何在 Node JS中使用Slack获取代码参数和oAuth进程?

转载 作者:行者123 更新时间:2023-11-29 21:17:05 25 4
gpt4 key购买 nike

我正在尝试为 slack 开发一个应用程序。我想通过松弛按钮获取代码参数以响应 oauth 流程发送,但我不知道如何获取参数。

事实上,我首先发送了按钮,然后有人可以点击它并在其松弛 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/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');
});

最佳答案

您说您想要获取代码 - 在用户点击您的添加到 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进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com