简体   繁体   中英

Zapier JS Action to Fetch Klout Scores

I'm trying to create a Java Script Code Action on Zapier to fetch Klout Scores for any given Twitter user name...

I've realized that this needs to be done in 2 stages:

1) First get the Klout ID for any Twitter screen_name:

http://api.klout.com/v2/identity.json/twitter?screenName="+screen_name+"&key="+klout_apikey"

Klout replies back to that with JSon:

{"id":"85568398087870011","network":"ks"}

2) second get the Klout score for that Klout id:

http://api.klout.com/v2/user.json/"+klout.id+"/score?key="+klout_apikey"

Klout replies back to this with JSon:

{"score":65.68382904221806,"scoreDelta":{"dayChange":-0.03663891859041257,"weekChange":-0.5495711661078815,"monthChange":-1.4045672671990417},"bucket":"60-69"}

Of course, what I need is the "score":65.68382904221806 object of the JSon reply array.

I use these following JS functions proposed by @KayCee:

 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.

In the "input data" section of the Zapier code action i pass the screen_name as a variable:

screen_name: [the twitter handle]

What I get back is the following error message:

SyntaxError: Invalid or unexpected token

What is the error that you see? You could do this by simply using the fetch client. You might want to remove the variable declarations before adding this to the code step.

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.

Refer to their documentation here - https://zapier.com/help/code/#introductory-http-example

Also refer to their Store client which allows you to store values (for cache) - https://zapier.com/help/code/#storeclient-javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM