Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid topicName does not match

I have been trying to solve this problem all the day but I can't find where the error. I am making a program to detect when I receive an mail in my gmail and write that mail in a Google Spreadsheet, I am using the Gmail API and Google Apps Script. This is my code:

gs code is :

function doPost(e) {
  var message = JSON.parse(e.postData.getDataAsString()).message
  var data = Utilities.newBlob(Utilies.base64Decode(message.data)).getDataAsString()[0];
  var ss = SpreadsheetApp.openById('1b8s5PLItCsmk8l1q0T1KHYOzjW7iDv4sRXSFvAxVFbQ').getSheets()[0];
  ss.appendRow([new Date(), message.message_id,data]);
  return 200;
}

function capturemail(){

  var WatchRes = Gmail.newWatchRequest();
  WatchRes.labelIds = ["INBOX"];
  //WatchRes.labelFilterAction = "include";
  WatchRes.topicName = "projects/proyecgmailyou/topics/mailsuc";

  var response = Gmail.Users.watch(WatchRes,"[email protected]");

  Logger.log(response);

}

The function doPost() I put in a web: https://script.google.com/a/uc.cl/macros/s/AKfycby8gOrWrMDkaAlgNdXNHl2J424Hvv0yu2CKKhJQW41Ka3Xa55g/exec

and then I try to run the function capturemail but appear the next error :

The API call to gmail.users.watch failed with the error: Invalid topicName does not match projects/sys-72285619869091378116913905/topics/* 

and is so strange because i never use this name"sys-72285619869091378116913905" and i didn t see it in same other place

also i give the permission to [email protected] enter image description here

I think that the problem was the id of the project so I review that part and this is what I find: enter image description here

I also try to change the topicName for :

projects/sys-72285619869091378116913905/topics/mailsuc

but appear this other error:

The API call to gmail.users.watch failed with the error: Error sending test message to Cloud PubSub projects/sys-72285619869091378116913905/topics/mailsuc : Resource not found (resource=mailsuc).

however I am sure that I have that resource create because I did here:

enter image description here

and the last thing i used this video of youtube to guide me youtube.com/watch?v=wjHp9_NAEJo

like image 992
mrpepo877 Avatar asked May 21 '19 20:05

mrpepo877


2 Answers

There are new steps to change the Cloud Project for your Apps Script to a "standard" Cloud Platform project (if you're using the "new editor"):

  1. Open the script whose GCP project you want to replace.
  2. At the left, click Project Settings settings.
  3. Under Google Cloud Platform (GCP) Project, click Change project.
  4. Enter the new project number and click Set project.
like image 23
gulima Avatar answered Nov 13 '22 10:11

gulima


The watch function documentation has this to say about the project:

Note that the "my-project-identifier" portion must exactly match your Google developer project id (the one executing this watch request).

sys-72285619869091378116913905 is presumably the project ID of the project that the Apps Script is running as. This is a "default Cloud Project" as described here. This obviously doesn't match proyecgmailyou.

To fix this, you can change the Cloud Project for your Apps Script to a "standard" Cloud Platform project with the following steps:

  1. Open the script editor for your script
  2. Go to "Resources > Cloud Platform Project..."
  3. Enter your cloud project number there (ie 729073306366)

Authorizations will be lost, but after that the Apps script will be running in your cloud project and so can use a topic from that project. Note: With the new App Script editor you should follow these steps ;

  1. Open Editor and
  2. Choose Google Cloud (GCP) Project and give your project number where you defined the pub/sub topic.

click on settings from left side menus

like image 195
David Avatar answered Nov 13 '22 09:11

David