以下代码使用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) ->
...
答案 0 :(得分:1)
成功获取客户端然后运行后错误浮出水面:
client.analytics.data.ga.get(queryObj).execute (err, result) -> ....
API客户端的撰稿人Ryan Seys建议here .discover
应调用一次,结果client
应重复使用。我连续数百次呼叫.discover
并创建了一堆新的client
。服务器可能不喜欢这样。通过存储和重用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) ->