下面的代码调用谷歌分析报告API使用谷歌的nodejs客户端版本0.7
在这里。它在某些执行时返回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) ->
...
客户端运行成功后出现错误:client.analytics.data.ga.get(queryObj).execute (err, result) -> ....
.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) ->