ces*_*pon 2 php gmail push-notification gmail-api google-cloud-pubsub
我想设置推送通知手表,但收到错误回复.我需要什么授权?
请求:
// 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"]
))
));
Run Code Online (Sandbox Code Playgroud)
响应:
{
"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."
}
}
Run Code Online (Sandbox Code Playgroud)
更多细节:
GMAIL_READONLY.不要添加allAuthenticatedUsers或allUsers,这将使您的主题公开。您可能已经看到此警告
该资源是公共的,互联网上的任何人都可以访问。要删除公共访问权限,请从资源的成员中删除“allUsers”和“allAuthenticatedUsers”。
相反添加gmail-api-push@system.gserviceaccount.com. 这会起作用。参考:https ://developers.google.com/gmail/api/guides/push#grant_publish_rights_on_your_topic
从页面: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 Developer Console权限界面执行此操作.
(重点补充)
您必须授予主题权限。转到您的主题列表或单击以下链接https://console.cloud.google.com/cloudpubsub/topic。
然后输入新成员电子邮件或如果您的应用程序有多个用户,则您可以输入allUsers。然后选择角色Pub/Sub Publisher并单击Save按钮。
注意:这将使您的主题公开。
