Google Cloud PubSub:以前有效的订阅突然未经授权

dun*_*aus 5 oauth node.js google-cloud-platform google-cloud-pubsub

我在使用Google Cloud PubSub API时遇到了一些问题.最近,我开始使用Cloud PubSub为我正在处理的聊天服务器排队消息.来自PubSub订阅的消息将使用socket.io节点库转发给用户.我没有遇到这个设置问题 - 我运行我的node.js服务器,打开几个浏览器窗口,我可以毫无问题地聊天.

但是,我注意到,经常在服务器运行几个小时后,它开始吐出以下错误:

(node:2525) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (node:2525) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

...

(node:2525) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1253): Error: Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential...

此错误重复并重复(您可以看到rejection id错误消息中的递增),直到几分钟后停止并恢复正常工作.当我收到错误消息时,我无法通过Cloud PubSub发送任何消息.

这是设置监听器的代码:

function listenForMessages(handler)
{
    var pubsub = require('@google-cloud/pubsub')({
        projectId: config.pubsub_project_id,
        keyFilename: config.pubsub_keyfile
    });

    pubsub.subscribe(config.pubsub_topic, 'test-subscription', {autoAck: true}, function(err, subscription){
        subscription.on('message', function(message) {
            handler(message.data);
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

凭据来自外部配置文件 - 我很确定它们没问题,特别是考虑到我最初运行服务器时设置监听器没有问题.

TL; DR:在我开始运行使用Google Cloud PubSub对邮件进行排队的节点服务器几小时后,我开始反复出现"无效凭据"错误.

viz*_*tiz 1

我面临着同样的问题,终于找到了解决方案。问题是 google 令牌过期,因此 pubsub 订阅者抛出异常。github 上已经存在一个类似的 oauth 问题的错误。(关联

该问题已在node-pubsub版本 0.20.0中修复,升级到此版本将解决您的问题。实际修复是在google-auth-library-nodejs 2.0 版本中并node-pubsub:v0.20.0使用google-auth-library-nodejs:2.0.

来自 github 发行说明google-auth-library-nodejs:2.0

OAuth2.refreshAccessToken 方法已被弃用。如果需要,getAccessToken、getRequestMetadata 和 request 方法都会自动刷新令牌。无需手动刷新令牌。

希望这可以帮助 !!