4

0.7 下面的代码在此处使用 Google 的 nodejs 客户端版本调用 Google Analytics Reporting API 。socket hang up它会在某些执行时返回错误,但并非总是如此。这会是谷歌服务器端的错误吗?有没有简单的调试方法?顺便说一句,我连续打了几个电话,不确定是否是由速率限制引起的。

gapi = require "googleapis"
authClient = new gapi.auth.JWT(
  config.ga.clientEmail,
  config.ga.privateKeyPath,
  null,
  [config.ga.scopeUri]
)

authPromise = new Promise (resolve, reject) ->
  authClient.authorize (err, token) ->
      resolve token
    return
  return

authPromise.then ->
  gapi.discover('analytics', 'v3')
    .withAuthClient(authClient)
    .execute (err, client) ->
      ...
4

1 回答 1

1

成功让客户端运行后出现错误: client.analytics.data.ga.get(queryObj).execute (err, result) -> ....

API 客户端的贡献者 Ryan Seys 在这里建议.discover应该调用一次,结果client应该重用。我连续调用.discover了数百次并创建了一堆新client的s。服务器可能不喜欢那样。通过存储和重用client问题就解决了。繁荣的工作代码:

gapi = require "googleapis"
authClient = new gapi.auth.JWT(
  config.ga.clientEmail,
  config.ga.privateKeyPath,
  null,
  [config.ga.scopeUri]
)

authPromise = new Promise (resolve, reject) ->
  authClient.authorize (err, token) ->
    gapi.discover('analytics', 'v3').withAuthClient(authClient).execute (err, client) ->
      resolve client
      return
    return
  return

authPromise.then (client) ->
  client.analytics.data.ga.get(queryObj).execute (err, result) ->
于 2014-06-11T06:41:11.617 回答