2016-08-17 10 views
2

私は余裕のためのアプリを開発しようとしています。 私は、スラックボタンでoauthフローへの回答でコードパラメータを送信したいと思いますが、パラメータを取得する方法がわかりません。ノードJSのスラックでoAuthプロセスでcodeパラメータを取得するにはどうすればよいですか?

は、実際に私は、最初のボタンを送った後、誰かが、私は] &状態を取得するためにwan'tその後、OAuthのフローはURLが https://www.myappname.com/oauth/?code=[parameterで新しいWebページに私をリダイレクトし、そのたるみチャンネルでアプリを実装し、それをクリックすることができます=

問題は、私のコードパラメータを取得する方法がリダイレクトを待つことではないということです。ここで

は私のコードです:

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の緩みから、あなたのアプリにURLパラメータとして送信されますユーザがSlackに追加ボタンをクリックした後、Slackのアプリケーションのインストール要求を承認した後、あなたのアプリは、router.get('/', function(request, response){};のスラックからのリクエストを待ち、request.urlを使用してコードを含む文字列にアクセスします。

いくつかの文字列操作を使用すると、URLからコード値を抽出し、クライアントのaccess_tokenは用のコードを交換するSlackのauth.access(client_id, client_secret, code)要求の中を呼び出すことができます。この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

を好む私はあなたが[ 'req.query']を使用して要求からアクセスコードを取得することができます – ghosteins

+1

私のスクリプトを変更し、その作業を知っていましたありがとうございました(http://expressjs.com/en/4x/api.html#req.query)。 – usandfriends

関連する問題