繁体   English   中英

错误 403:向 Cloud PubSub 发送测试消息时出错:用户无权执行此操作

[英]Error 403: Error sending test message to Cloud PubSub: User not authorized to perform this action

我想设置推送通知手表,但收到错误响应。 我需要什么授权?

请求:

// Google API
$client = getClient();

// POST request    
$ch = curl_init('https://www.googleapis.com/gmail/v1/users/me/watch');

curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer ' . $client->getAccessToken()['access_token'],
        'Content-Type: application/json'
    ),
    CURLOPT_POSTFIELDS => json_encode(array(
        'topicName' => 'projects/xxxx/topics/xxxx',
        'labelIds' => ["INBOX"]
    ))
));

回应:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Error sending test message to Cloud PubSub projects/xxxx/topics/xxxx : User not authorized to perform this action."
   }
  ],
  "code": 403,
  "message": "Error sending test message to Cloud PubSub projects/xxxx/topics/xxxx : User not authorized to perform this action."
 }
}

更多详情:

  • 使用的范围是GMAIL_READONLY
  • 订阅和主题存在并且它们是在同一个控制台中创建的。
  • 我尝试从控制台发布一条新消息,并且成功了。

从页面: https : //developers.google.com/gmail/api/guides/push#grant_publish_rights_on_your_topic

Cloud Pub/Sub 要求您授予 Gmail 权限以向您的主题发布通知。

为此,您需要向 serviceAccount:gmail-api-push@system.gserviceaccount.com 授予发布权限。 您可以按照资源级访问控制说明使用 Cloud Pub/Sub 开发者控制台权限界面执行此操作。

(强调)

您必须授予主题权限。 转到您的主题列表或单击以下链接https://console.cloud.google.com/cloudpubsub/topic

然后点击你的主题在此处输入图片说明

然后在右侧的权限选项卡中,单击添加成员按钮在此处输入图片说明

然后输入新成员电子邮件或如果您的应用程序有多个用户,则您可以输入allUsers 然后选择角色Pub/Sub Publisher并单击Save按钮。
注意:这将使您的主题公开。 在此处输入图片说明

不要添加allAuthenticatedUsersallUsers ,这将使您的主题公开。 您可能已经看到此警告

该资源是公开的,互联网上的任何人都可以访问。 要删除公共访问,请从资源的成员中删除“allUsers”和“allAuthenticatedUsers”。

所以不要这样做


而是添加gmail-api-push@system.gserviceaccount.com 这会起作用。 参考: https : //developers.google.com/gmail/api/guides/push#grant_publish_rights_on_your_topic

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM