【问题标题】:"socket hang up" error when calling Google API via node client通过节点客户端调用 Google API 时出现“套接字挂断”错误
【发布时间】:2014-08-01 02:43:38
【问题描述】:

以下代码使用 Google 的 nodejs 客户端版本 0.7 here 调用 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) ->
      ...

【问题讨论】:

    标签: node.js coffeescript google-api google-analytics-api google-api-nodejs-client


    【解决方案1】:

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

    API 客户端的贡献者 Ryan Seys 建议 here 应该调用一次 .discover,然后重用生成的 client。我连续数百次调用.discover 并创建了一堆新的clients。服务器可能不喜欢那样。通过存储和重用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-08-22
      • 2015-05-03
      • 2019-06-09
      • 1970-01-01
      • 2015-10-30
      • 2016-04-18
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      相关资源
      最近更新 更多