简体   繁体   中英

MesageID Android Gmail Api

How can I get the messageID for reading a specific mail using the Gmail Api in Android ?

The users.messages.get method of Gmail Api requires two parameters:

  1. The userId that will be the username
  2. The messageID

So, how is it possible to get the messageID and what actually is the messageID ?

To get messageIds you first need to list messages with some optional parameters. This will return messageIds which just are unique strings representing messages.

An example with regular http-requests would be:

Give me just one messageId from messages with the INBOX-label that are sent from myself

userId = me
labelIds = INBOX
maxResults = 1
q = from:me

GET https://www.googleapis.com/gmail/v1/users/me/messages?labelIds=INBOX&maxResults=1&q=from%3Ame

Response:

{
 "messages": [
  {
   "id": "14f8d57248451a6c", // This is the messageId!
   "threadId": "14f8d57248451a6c"
  }
 ],
 "nextPageToken": "04016634599566360443",
 "resultSizeEstimate": 2
}

I then use this messageId in the get-method to get the actual content:

GET https://www.googleapis.com/gmail/v1/users/me/messages/14f8d57248451a6c

Response:

{
 "id": "14f8d57248451a6c",
 "threadId": "14f8d57248451a6c",
 "labelIds": [
  "SENT",
  "INBOX",
  "IMPORTANT"
 ],
 "snippet": "",
 "historyId": "563949",
 "internalDate": "1441185342000",
 "payload": {
  "mimeType": "multipart/mixed",
  "filename": "",
  "headers": [
   {
    "name": "MIME-Version",
    "value": "1.0"
   },
   {
    "name": "Received",
    "value": "by 10.28.99.138 with HTTP; Wed, 2 Sep 2015 02:15:42 -0700 (PDT)"
   },
   {
    "name": "Date",
    "value": "Wed, 2 Sep 2015 11:15:42 +0200"
   }, ...

The Android Quickstart can help you a great deal if you want to use a nice library instead of doing the requests yourself.

You can list all the messages and from there you can get the MessageID.

public List<Message> listAllMessages(Gmail service, String userId
) throws IOException {

    ListMessagesResponse response = service.users().messages().list(userId).execute();
    if (response == null || response.isEmpty())
        return null;

    List<Message> messages = new ArrayList<Message>();
    messages.addAll(response.getMessages());

    int totalMsgs = messages.size();
    Message message;
    if (totalMsgs > 0) {
        for (int i = 0; i < totalMsgs; i++) { 
            message = messages.get(i); //message.getId() is what you want
        }
    }
    return messages;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM