Search code examples
javascriptjquerypythonhtmlgmail-api

Check if mail is read, gmail api


I am working on a gmail app (you can probably tell if you read my other questions), I can list my first 10 email, however I have no way of knowing weather they are read or not. Does anyone know how I would even go about this? Thanks!


Solution

  • If I list my newest message:

    Request

    GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=1&access_token={ACCESS_TOKEN}
    

    Response

    {
     "messages": [
      {
       "id": "158d822014b7887b",
       "threadId": "158d822014b7887b"
      }
     ],
     "nextPageToken": "17683191771541399341",
     "resultSizeEstimate": 2
    }
    

    And then get the message:

    Request

    GET https://www.googleapis.com/gmail/v1/users/me/messages/158d822014b7887b?format=metadata&access_token={ACCESS_TOKEN}
    

    Response

    {
     "id": "158d822014b7887b",
     "threadId": "158d822014b7887b",
     "labelIds": [
      "UNREAD",
      "CATEGORY_SOCIAL",
      "INBOX"
     ],
     ...
    }
    

    You can see that the message has a labelId with the value of UNREAD. If the message doesn't have this labelId, it is read.

    var isRead = message.labelIds.indexOf("UNREAD") === -1;