下面的代码使用Google的nodejs客户端版本0.7 here调用Google Analytics Reporting API.它会在某些执行时返回套接字挂起错误,但并非总是如此.这会是谷歌服务器端的错误吗?有一种简单的调试方法
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) – > ….
API客户端的贡献者Ryan Seys建议here应该调用.discover一次,并重新使用生成的客户端.我连续几次打电话.发现了一堆新客户.服务器可能不喜欢这样.通过存储和重用客户端,问题就消失了.繁荣的工作代码:
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) ->