【问题标题】:Zapier JS Action to Fetch Klout ScoresZapier JS Action 获取 Klout 分数
【发布时间】:2018-04-20 09:56:25
【问题描述】:

我正在尝试在 Zapier 上创建一个 Java 脚本代码操作,以获取任何给定 Twitter 用户名的 Klout 分数...

我意识到这需要分两个阶段完成:

1) 首先获取任何 Twitter 屏幕名称的 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

【问题讨论】:

    标签: zapier klout


    【解决方案1】:

    您看到的错误是什么?您可以通过简单地使用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

    【讨论】:

    • 尝试运行它,但现在我收到以下错误消息:发送您的测试时遇到问题。请再试一次。错误:ReferenceError:未定义屏幕名称
    • 您是否在输入数据中设置了 screen_name 变量?是否传递了一个值?
    • 我确实将 screen_name 设置为变量并且它确实通过了它。奇怪的是,我看到的是对 Klout 有两个单独的 API 调用...第一个向 Klout 发送 Twitter 用户名并接收回一个 Klout ID(该 ID 需要以某种方式临时兑现),第二个 API 调用Klout 应该通过 Klout ID 并取回 Klout 分数。为了确保问题不在于 Zapier(没有传递 screen_name),我在 JS 代码中硬编码了 Twitter 用户名,但它仍然没有工作......?
    • 为了更好地理解这一点,您能否使用您现在拥有的代码更新您的问题?
    • 完成...我已经用我现在使用的代码(你的建议)更新了我的问题,以及我从 Zapier 收到的确切(新)错误消息
    猜你喜欢
    • 2021-02-04
    • 2022-11-24
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    相关资源
    最近更新 更多