Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

gmail

api

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.

like image 992
Diego Jovanovič Avatar asked Feb 13 '23 15:02

Diego Jovanovič


2 Answers

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.

like image 177
ADW Avatar answered Feb 15 '23 09:02

ADW


Well, I have used:

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

It is working :)

like image 30
Diego Jovanovič Avatar answered Feb 15 '23 10:02

Diego Jovanovič