node js将返回的API密钥存储为变量

时间:2017-06-08 16:07:36

标签: javascript node.js api express servlets

我是节点新手,我正在尝试使用A node.js library for the Pardot API

我将userKeyemailpassword传递给pardot,这会返回api_key

我使用以下代码进行身份验证,然后当我运行我的节点服务器时,我可以看到api_key被传回。

如何将api_key存储为变量然后用于将数据发布到API?

我的节点代码

var nodePardot = require('node-pardot');

// Create client using email, password and user_key
nodePardot.PardotAPI({
    userKey: 'kyujk',
    email: 'uu.uuy@uu.co.uk',
    password: 'uit.09',
    DEBUG: true
}, function(err, client) {
    // Authentication completed
    console.log('Authentication completed');

    // gets api key
    var api_key = api_key;
    console.log('key',api_key);
});

我的控制台输出

[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Server started! At http://localhost:8080
Initializing node-pardot in DEBUG mode
params {"email":"bhyuyten.jj@jyj.co.uk","password":"tyjt.09","user_key":"yjty76","format":"json"}
uri https://pi.pardot.com/api/login/version/3?email=ss.jyj%s.co.uk&password=tyjt.09&user_key=yjty76&api_key=&format=json
parsedResponse {"@attributes":{"stat":"ok","version":1},"api_key":"878074492492dhjjk67u","version":4}
Authentication completed
key undefined
parsedResponse {"@attributes":{"stat":"ok","version":1},"api_key":"878074492492dhjjk67u","version":4}


var nodePardot = require('node-pardot');

    // Create client using email, password and user_key
    nodePardot.PardotAPI({
        userKey: 'fdgr',
        email: 'rg.gg@gg.co.uk',
        password: 'dfherg',
        DEBUG: true
    }, function(err, client) {
        // Authentication completed
        console.log('Authentication completed');

        // gets api key
        var api_key = api_key;
        console.log('key',api_key);
    });

2 个答案:

答案 0 :(得分:1)

查看node-pardot [{3}}和here的来源,看起来API Key位于client.apiKey

此外,您应该始终在nodejs样式的回调函数中检查错误情况!

nodePardot.PardotAPI({
    userKey: '...',
    email: '...',
    password: '...',
    DEBUG: true
}, function(err, client) {
    if(err){
        // Authentication failed
        // handle error
        console.error("Authentication Failed", err)
    } else {
        // Authentication successful
        // gets api key
        var api_key = client.apiKey;
        console.log("Authentication successful", api_key);
    }
});

P.S。您永远不应在公共论坛和QA网站上发布API密钥和凭据

答案 1 :(得分:0)

您似乎正试图在

中将未声明变量的值分配给自身
var api_key = api_key;

您应该在响应中为其指定值,即

var api_key = client.api_key;