如何在节点JS中使用Slack获取代码参数和oAuth进程?

时间:2016-08-17 14:14:54

标签: javascript node.js oauth slack

我正在尝试开发一款应用程序。 我想通过松弛按钮获取代码参数send以回应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');
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您说您想获取代码 - 用户点击添加到Slack后,访问代码会在GET请求中作为 url 参数从Slack发送到您的应用/ em>按钮并授权Slack的安装应用程序的请求。您的应用会在router.get('/', function(request, response){};中等待来自Slack的这些请求,并使用 request.url 来访问包含该代码的字符串。

通过一些字符串操作,您可以从网址中提取代码值,并在请求中调用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
   }
});