Get Label ID to get a List of Messages with GMAIL API

元气小坏坏 提交于 2020-02-02 11:32:10

问题


I am using the new GMAIL API v1 that Google launched some days ago.

I want to get the list of some emails filtering with some labels. If I want to do that, I need to get the label ID of each "label_name" but I just can get the list of all of labels...

Any idea?

Diego.


回答1:


Well, I have used:

ListMessagesResponse messagesWithLabels = service.users().messages().list("me").setQ("label:mylabel").execute();

It is working :)




回答2:


LABEL ID is the same LABEL NAME.

{
   "id": "INBOX",
   "name": "INBOX",
   "messageListVisibility": "hide",
   "labelListVisibility": "labelShow",
   "type": "system"
  }

try here API Explorer with your credentials.




回答3:


Label ID is the same as Label Name only for system generated labels. For example CHAT, SENT, INBOX, TRASH, etc.

User-generated labels have IDs that are different from their names.

The easiest way to get them is to use the API explorer that imCaps mentioned.

Alternatively, you can use this

function listLabels() {
  var request = Gmail.Users.Labels.list('me');
  var name, id;
  for (var l = 0 ; l < request.labels.length; l++) {
    name = request.labels[l].name;
    id = request.labels[l].id;
    Logger.log("%s. %s %s", l, name, id)
  }
}

You'll need to enable the Gmail API in Advanced Google Services for this code to work.



来源:https://stackoverflow.com/questions/24484970/get-label-id-to-get-a-list-of-messages-with-gmail-api

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