使用 express 框架在 pipeDrive 回调中进行 OAuth 授权

OAuth Authorization in pipeDrive callback using express framework

我在 Marketplace Manager 中的 sandbox.pipedrive.com 上创建了一个应用程序,然后我创建了一个回调,要求用户安装我在 pipedrive 中设置的应用程序。

如果用户允许安装,他们将被重定向到我在控制器中的回调 url,我的控制器代码是 :-

app.get('/pipedrive-callback', function(req, res) {
    console.log('Success')
});

现在我想交换授权令牌。谁能帮我解决这个问题。

你能试试这个吗? 在用户被重定向到您的回调后,您需要向他们的服务器发送另一个 post 请求。重定向后,您将从请求参数中获得 authorization_code。您必须在此 post 请求中发送该代码,以获取允许您施展魔法的实际令牌。

app.get('/pipedrive-callback', function (req, res) {
    console.log('Success');
    const authorization_code_from_service = req.query.code; // This will extract the authorization_code from the call back url.

    //Here goes your step 4 + 5. You need to make a post request to their server now. For this, there is a library aka "request" in npm. 
    // Here is the link for that https://www.npmjs.com/package/request

    const request = require("request");

    const formData = {
        "grant_type": "authorization_code",
        "redirect_uri": "rediect url that you have set for your app",
        "code": authorization_code_from_service
    }


    request.post({
            url: 'https://oauth.pipedrive.com/oauth/token',
            form: formData
        },
        function (err, httpResponse, body) {
            //This will be the data that you need for further steps. Actual token, expiry time etc
            console.log(body);
        }
    );

});

Npm link : https://www.npmjs.com/package/request