简体   繁体   English

MesageID Android Gmail Api

[英]MesageID Android Gmail Api

How can I get the messageID for reading a specific mail using the Gmail Api in Android ? 如何使用Android中的Gmail Api获取用于阅读特定邮件的messageID

The users.messages.get method of Gmail Api requires two parameters: Gmail Api的users.messages.get方法需要两个参数:

  1. The userId that will be the username userId将是用户名
  2. The messageID messageID

So, how is it possible to get the messageID and what actually is the messageID ? 那么,怎么可能得到messageID和什么是真正的messageID

To get messageIds you first need to list messages with some optional parameters. 要获取messageIds ,首先需要列出带有一些可选参数的消息 This will return messageIds which just are unique strings representing messages. 这将返回messageIds ,它只是表示消息的唯一字符串。

An example with regular http-requests would be: 常规http请求的示例如下:

Give me just one messageId from messages with the INBOX-label that are sent from myself 从我自己发送的带有INBOX标签的邮件中只给我一条messageId

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-method中使用此messageId来获取实际内容:

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. 如果你想使用一个漂亮的库而不是自己做请求, Android Quickstart可以帮助你很多。

You can list all the messages and from there you can get the MessageID. 您可以列出所有消息,然后您可以从中获取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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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