2016-08-17 11 views
2

我正試圖開發一個應用程序鬆弛。 我想獲得代碼參數發送回答oauth流與鬆弛按鈕,但我不知道如何獲取參數。如何使用節點JS中的Slack獲取oAuth進程中的代碼參數?

其實我首先發送按鈕,然後有人可以點擊工具上的鬆弛通道的應用程序,然後將OAuth的流量重定向我到一個新的網頁的網址是哪 https://www.myappname.com/oauth/?code=[parameter我wan't獲得] &狀態=

問題是我的方法獲取代碼參數不等待重定向。

這裏是我的代碼:

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/[email protected] 2x" /></a>'); 
 

 
     console.log('cb0:le bouton slack s\'affiche'); 
 
    router.get('/oauth/',recupCode); 
 
}; 
 

 
router.get('/',boutonSlack); 
 
app.listen(port, function() { 
 
    console.log('Ready'); 
 
});

回答

2

你說你想獲得的代碼 - 訪問代碼被髮送作爲從鬆弛一個網址參數,您的應用程序在一個GET請求後,用戶點擊您的添加到Slack按鈕並授權Slack的請求來安裝您的應用程序。您的應用程序會等待這些來自Slack的請求router.get('/', function(request, response){};,並且您使用request.url來訪問包含代碼的字符串。

對於某些字符串操作你可以從URL中提取的代碼價值,並呼籲鬆弛的auth.access(client_id, client_secret, code)的請求,以獲得客戶的交流的access_token的代碼。這個access_token是你用來做一切事情的一個團隊,所以你需要存儲它。

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

按鈕通常顯示的網站和節點應用作爲服務器等待鬆弛來授權請求​​上。

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 
    } 
}); 
+1

謝謝你,我已經改變我的腳本,並瞭解其工作 – ghosteins

+1

可以使用得到請求接入碼['req.query'] (http://expressjs.com/en/4x/api.html#req.query)。 – usandfriends

相關問題