NodeJS Patreon API帐户链接

时间:2016-11-03 00:25:28

标签: node.js api post oauth patreon

我试图将我网站上的用户帐户连接到patreon。我一直收到一条access_denied错误消息,以响应第3步。我跟随此documentation

我的节点服务器代码如下所示:

  socket.on("patreon_register",function(code,user){
        var reqString = "api.patreon.com/oauth2/token?code="
                        +code
                        +"&grant_type=authorization_code&client_id="
                        +settings.patreon.Client_ID
                        +"&client_secret="
                        +settings.patreon.Client_Secret
                        +"&redirect_uri="
                        +"http%3A%2F%2Fwww.levisinger.com%2F%3Fpage%3Dpatreon_success",                        
        req = querystring.stringify({
            "code": code,
            "grant_type": "authorization_code",
            "client_id": settings.patreon.Client_ID,
            "client_secret": settings.patreon.Client_Secret,
            "redirect_uri": "http%3A%2F%2Fwww.levisinger.com%2F%3Fpage%3Dpatreon_success"
            }),
        post_options = {
                host: 'api.patreon.com',
                port: '80',
                path: '/oauth2/token',
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': Buffer.byteLength(req)
                }
            };        
        // Set up the request
        console.log(req);
        var post_req = http.request(post_options, function(res) {
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log(chunk);
                if(
                    chunk.access_token &&
                    chunk.refresh_token &&
                    chunk.expires_in &&
                    chunk.scope &&
                    chunk.token_type
                ){
                    Auth.linkPatreon(user,chunk,function(err,res){
                        if(err){ socket.emit('patreon_register',false,res); }
                        else { socket.emit('patreon_register',true,res); }
                    });
                }
            });
        });
        // post the data
        post_req.write(req);
        post_req.end();
    });    

实际发送到服务器的req变量如下所示(当然将我的代码更改为通用值)

code=MY_RESPONSE_CODE&grant_type=authorization_code&client_id=MY_CLIENT_ID&client_secret=MY_CLIENT_SECRET&redirect_uri=MY_RESPONSE_URI

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

最后,我的服务器看起来像这样并且正在运行:

socket.on("patreon_register",function(code,user){
    var req = querystring.stringify({
            code: code,
            grant_type: "authorization_code",
            client_id: settings.patreon.Client_ID,
            client_secret: settings.patreon.Client_Secret,
            redirect_uri: settings.patreon.redirect_uri
        }),
    post_options = {
            host: 'api.patreon.com',
            port: '80',
            path: '/oauth2/token',
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': Buffer.byteLength(req)
            }
        };        
    // Set up the request
    console.log(req);
    var post_req = http.request(post_options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            chunk = JSON.parse(chunk);
            console.log(chunk);
            if(!chunk["error"]){
                console.log("Linking!");
                Auth.linkPatreon(user,chunk,function(err,res){
                    if(err){ socket.emit('patreon_register',false,res); }
                    else { socket.emit('patreon_register',true,res); }
                    console.log("Linked!");
                });
            }
        });
    });