Gmail API Watch not filtering by Label

Deadly 提交于 2019-12-28 15:15:21

问题


I am using Gmail Push Notifications with Google PubSub and have a custom label that I want to monitor for any changes. I use the following code to register a watch for the label (Id of the label is Label_1)

WatchRequest wr = new WatchRequest();
wr.TopicName = "projects/" + primaryLink.ggProjectId + "/topics/iLink" + segmentId;
if (labels != null && labels.Count > 0)
{
    wr.LabelIds = new List<string>();
    wr.LabelIds.Add("Label_1");
    wr.LabelFilterAction = "include";
}

WatchResponse wrr = gs.Users.Watch(wr, emailAccount).Execute();
return "HistoryId " + wrr.HistoryId.ToString();

}

The watch registers OK. The issue is that I get push notifications for any Gmail change not just those under the label.

Are custom labels supported?


回答1:


I noticed the same issue but later on found out that its because of the way API works. You can filter the emails via LabelIds but you will receive notifications only if emails are directly being filtered to selected custom label. I guess its design rather than a flaw in the API.

To test this, create a custom filter in Gmail which would directly apply your custom label to a set of emails and you should be receiving notifications for those emails.

Edited (June 11, 2015): Push notification send you HistoryID and user's mailbox name. In response your endpoint should call userhistory.list() with HistoryID and LabelId you want to monitor for changes.

$opt_param = array();
$opt_param['startHistoryId'] = $historyID;
$opt_param['labelId'] = $labelID;
$opt_param['fields'] = 'nextPageToken,historyId,history/messagesAdded';

$service->users_history->listUsersHistory($userID, $opt_param);

Above is a PHP code snippet to filter the history list with historyID and labelID.



来源:https://stackoverflow.com/questions/30591302/gmail-api-watch-not-filtering-by-label

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!