我试图在Zapier上创建一个Java脚本代码动作,为任何给定的Twitter用户名获取Klout分数.
我意识到这需要分两个阶段来完成:
1)首先获取任何Twitter screen_name的Klout ID:
http://api.klout.com/v2/identity.json/twitter?screenName="+screen_name+"&key="+klout_apikey"Klout用JSon回复:
{"id":"85568398087870011","network":"ks"}2)第二,得到Klout id的Klout分数:
http://api.klout.com/v2/user.json/"+klout.id+"/score?key="+klout_apikey"Klout用JSon回复这个问题:
{"score":65.68382904221806,"scoreDelta":{"dayChange":-0.03663891859041257,"weekChange":-0.5495711661078815,"monthChange":-1.4045672671990417},"bucket":"60-69"}当然,我需要的是JSon回复数组的“得分”:65.68382904221806对象。
我使用@KayCee提出的以下JS函数:
var klout_apikey = '<my klout api key>';
fetch("http://api.klout.com/v2/identity.json/twitter?screenName="+screen_name+"&key="+klout_apikey")
.then(function(res) {
return res.json();
})
.then(function(klout) {
console.log(klout);
if(klout.id) {
return fetch("http://api.klout.com/v2/user.json/"+klout.id+"/score?key="+klout_apikey")
}
}).then(function(res) {
return res.json();
}).then(function(body) {
// console.log(body.score);
//Here is where you are telling Zapier what you want to output.
callback(null, body.score)
}).catch(callback); //Required by Zapier for all asynchronous functions.在Zapier代码操作的“输入数据”部分,我将screen_name作为变量传递:
screen_name: [the twitter handle]我得到的是以下错误消息:
SyntaxError: Invalid or unexpected token发布于 2018-04-20 13:06:03
你看到了什么错误?您只需使用fetch客户机就可以做到这一点。在将变量声明添加到代码步骤之前,您可能希望删除这些变量声明。
var inputData = {'screen_name': 'jtimberlake'}
//Remove the line above before pasting in the Code step. You will need to configure it in the Zap.
var klout_apikey = '2gm5rt3hsdsdrzgvnskmgm'; //Not a real key
fetch("http://api.klout.com/v2/identity.json/twitter?screenName="+inputData.screen_name+"&key="+klout_apikey)
.then(function(res) {
return res.json();
})
.then(function(body) {
console.log(body);
if(body.id) {
return fetch("http://api.klout.com/v2/user.json/"+body.id+"/score?key="+klout_apikey)
}
}).then(function(res) {
return res.json();
}).then(function(body) {
console.log(body);
//Here is where you are telling Zapier what you want to output.
callback(null, body)
}).catch(callback); //Required by Zapier for all asynchronous functions.请参阅这里的文档- https://zapier.com/help/code/#introductory-http-example
还可以参考他们的Store客户端,它允许您存储值(用于缓存)- https://zapier.com/help/code/#storeclient-javascript
https://stackoverflow.com/questions/49938963
复制相似问题