簡體   English   中英

Zapier JS動作獲取Klout分數

[英]Zapier JS Action to Fetch Klout Scores

我正在嘗試在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回復數組的“ score”: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

您看到的錯誤是什么? 您可以通過簡單地使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM