0

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
Elad Ratson
  • 706
  • 1
  • 7
  • 23

1 Answers1

2

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

KayCee
  • 174
  • 1
  • 11
  • Tried running it but now I get the following error message: **We had trouble sending your test through. Please try again. Error: ReferenceError: screen_name is not defined** – Elad Ratson May 09 '18 at 15:29
  • Did you set the screen_name variable in input data? Is a value being passed to it? – KayCee May 09 '18 at 16:03
  • I indeed set the screen_name as a variable and it does pass it. Strangely enough, what I saw is that there are two separate API calls to Klout... The first sends to Klout a Twitter user name and receive back a Klout ID (that ID needs to be temporarily cashed somehow), the second API call to Klout should pass the Klout ID and get back the Klout score for it. To make sure the problem is not with Zapier (not passing the screen_name) I hard-coded the Twitter user_name in the JS code and still it didn't work...? – Elad Ratson May 10 '18 at 16:58
  • To understand this better, could you update your question with the code you have now? – KayCee May 10 '18 at 17:01
  • Done... I've updated my question with the code i'm using now (your proposal), and the exact (new) error message I get back from Zapier – Elad Ratson May 11 '18 at 00:19
  • @EladRatson I've updated my answer. Zapier puts the `screen_name` variable inside the `inputData` object. From your question, it also looks like you've added some extra quotes towards the end of the fetch URLs, these are not necessary. Copy and paste the code that I updated and it should work as expected. You can remove the first line - I added that only for testing outside of Zapier. – KayCee May 11 '18 at 14:54
  • Thank you @KayCee... i've updated the code and it seems we are advancing but this time I get the following Error Message: **Error: You must return a single object or array of objects.** – Elad Ratson May 13 '18 at 10:16
  • Ah! Sorry about that, it's a simple fix updating my answer. Zapier expects you to either return an object or an array of objects. – KayCee May 13 '18 at 10:28
  • But isn't the "callback(null, body.score)" a single object within the array ? – Elad Ratson May 13 '18 at 10:30
  • No that would be a string/int. You can use `callback(null, body)` and then you will be able to use score from that response in your next step. – KayCee May 13 '18 at 10:40
  • @EladRatson I've just tested this with a real API key on Zapier and it works perfectly. If this answer solved your problem, make sure to upvote it and select it as correct ([info here](https://stackoverflow.com/help/someone-answers)) – KayCee May 13 '18 at 11:08