Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to set up a push notification watch but I receive an error response. What authorization I need?

Request:

// 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"]
    ))
));

Response:

{
 "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."
 }
}

More details:

  • The scope used is GMAIL_READONLY.
  • The suscription and the topic exist and they were created in the same console.
  • I tried to publish a new message from the console and it has worked.
like image 217
cespon Avatar asked Sep 09 '17 17:09

cespon


People also ask

Why am I getting 403 (Forbidden) error in Google Cloud Platform?

Here are some notes come from google's documentation Troubleshooting 403 (Forbidden) error : Make sure you've enabled the Google Cloud Pub/Sub API in the Cloud Platform Console. communication. resource. See Google Cloud Dataflow Security and Permissions for more information.

What is the error code 403?

HTTP Error Response code: 403 Forbidden HTTP Error Response body: Error: Forbidden Your client does not have permission to get URL /<FUNCTION_NAME> from this server. Allow public (unauthenticated) access to all users for the specific function.

Why can't my Cloud Functions service access Cloud Pub/Sub?

If the role assigned to the service account is changed and the appropriate permissions are not otherwise granted, the Cloud Functions service cannot access Cloud Pub/Sub and the deployment fails. Reset this service account to the default role. Grant the pubsub.subscriptions.* and pubsub.topics.* permissions to your service account manually.

How to add principal permissions to subscription and topic?

In view of subscription and topic you can click "permission" -> "add principal", paste long e-mail from your json with service account and select permissions described in Following the answer by Daniel, I tried to give my self Pub/Sub Editor role Pub/Sub Publisher role but it did not work, what worked for me is Pub/Sub Admin


2 Answers

From the page: https://developers.google.com/gmail/api/guides/push#grant_publish_rights_on_your_topic

Cloud Pub/Sub requires that you grant Gmail privileges to publish notifications to your topic.

To do this, you need to grant publish privileges to serviceAccount:[email protected]. You can do this using the Cloud Pub/Sub Developer Console permissions interface following the resource-level access control instructions.

(emphasis added)

like image 103
payne Avatar answered Oct 04 '22 16:10

payne


You have to grant permission to topics. Go to your topics list Or click on the below link https://console.cloud.google.com/cloudpubsub/topic.

Then click on your topic enter image description here

Then in right side permission tab, click on the ADD MEMBER button enter image description here

Then enter the new member email or If your App have multiple user then you can enter allUsers. Then Select the role Pub/Sub Publisher and click on the Save button.
Note: This will make your topic public. enter image description here

like image 21
Sandeep Sherpur Avatar answered Oct 04 '22 16:10

Sandeep Sherpur