通过节点客户端调用Google API时出现“套接字挂起”错误

以下代码在此处使用Google的nodejs客户端版本0.7调用Google Analytics报告API。 它在一些执行中返回socket hang up错误,但并不总是如此。 这是否会成为Google服务器端的错误? 有没有简单的方法来debugging? 顺便说一句,我连续打几个电话,不知道是否是由速率限制造成的。

 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在这里build议.discover应该被调用一次,最终的client应该被重用。 我打电话,连续.discover数百次,并创build了一批新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) ->