-1

I'm retrieving messages from my Gmail using Gmail API. specifically, the email with Hangouts conversations using this url: https://www.googleapis.com/gmail/v1/users/me/messages?q=in:chats

When I enter in a message, I see this structure

 {
  "id": "1555561f7b8e1sdf56b",
  "threadId": "155552511dfsd83ce98",
  "labelIds": [
    "CHAT"
  ],
  "snippet": "df",
  "historyId": "270812",
  "internalDate": "1466016331704",
  "payload": {
    "partId": "",
    "mimeType": "text/html",
    "filename": "",
    "headers": [
      {
        "name": "From",
        "value": "\"Oscar J. Irún\" <Oscarjiv91@gmail.com>"
      }
    ],
    "body": {
      "size": 2,
      "data": "ZGY="
    }
  },
  "sizeEstimate": 100
}

as you can see, the body message is "df". Everything it's ok so far.

The problem comes when the Hangout message is an image. The snippet field is empty, and it doesnt show any attachment in the message. This is an example:

{
  "id": "155558233274d78c91",
  "threadId": "15fd5552511d83ce98",
  "labelIds": [
    "CHAT"
  ],
  "snippet": "",
  "historyId": "27sd0827",
  "internalDate": "1466018445133",
  "payload": {
    "mimeType": "text/html",
    "filename": "",
    "headers": [
      {
        "name": "From",
        "value": "\"Oscar J. Irún\" <Oscarjiv91@gmail.com>"
      }
    ],
    "body": {
      "size": 0,
      "data": ""
    }
  },
  "sizeEstimate": 100
}

I need to retrieve this inline images. Any help will be appreciated!

Oscar J. Irun
  • 455
  • 5
  • 17
  • what exactly is your question? are you asking how to get the URL of the image? – Woodrow Barlow Jun 15 '16 at 20:03
  • Sorry, yes @WoodrowBarlow. Edited my question – Oscar J. Irun Jun 15 '16 at 20:07
  • Getting chat messages through the Gmail API is [not supported](http://stackoverflow.com/questions/25316138/find-timestamp-for-hangout-and-chat-messages-retrieved-with-gmail-api), so getting attachments is probably not possible. – Tholle Jun 16 '16 at 18:58

2 Answers2

2

You can retrieve attachments by using Users.messages.attachments:get. Take note that this request requires authorization. All requests to the Gmail API must be authorized by an authenticated user. Gmail uses the OAuth 2.0 protocol for authenticating a Google account and authorizing access to user data.

HTTP request

GET https://www.googleapis.com/gmail/v1/users/userId/messages/messageId/attachments/id

public static void getAttachments(Gmail service, String userId, String messageId)
throws IOException {
Message message = service.users().messages().get(userId, messageId).execute();
List<MessagePart> parts = message.getPayload().getParts();
for (MessagePart part : parts) {
if (part.getFilename() != null && part.getFilename().length() > 0) {
String filename = part.getFilename();
String attId = part.getBody().getAttachmentId();
MessagePartBody attachPart = service.users().messages().attachment().
get(userId, messageId, attId).execute();
byte[] fileByteArray = Base64.decodeBase64(attachPart.getData());
FileOutputStream fileOutFile =
new FileOutputStream("directory_to_store_attachments" + filename);
fileOutFile.write(fileByteArray);
file OutFile.close();
}
}
}
Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30
  • Thanks but the problem is with the Hangouts messages. It appears empty, with no attachments or anything. This works for a normal email retrieved with Gmail API, but it doesn't with Hangouts messages. – Oscar J. Irun Jun 16 '16 at 17:21
0

JUST FYI for PHP the solution is something similar to this: base64_decode(strtr($gmail->service->users_messages_attachments->get('me', $message->id, $arrPart['body']['attachmentId'])->data,'-_', '+/'));

Ruttyj
  • 853
  • 2
  • 11
  • 18