9
votes

I was under the assumption that each attachment for a given message will have a unique attachmentId that I could use to reference that attachment when needed. Seems this assumption is incorrect.

Each time you call the Users.messages.get service for Google's GMail API ( https://developers.google.com/gmail/api/v1/reference/users/messages/get ) to retrieve a message that has an attachment, the returned attachmentId is different even though the same messageId was used. How would I find the "real" attachmentId?

2
Seems to me you must fetch that id every time ... since Google is changing it.Mihai Iorga
Could you elaborate on what you are trying to achieve? To my understanding the "old" attachmentIds still work even though you get a different one when you get the message the second time.Tholle
I am simply adding the attachment to a database, and the intention was to key on attachmentId. I don't want to re-add attachments that have already been added to my database, so I was checking for a record with the same attachmentId in my database before inserting. I noticed my DB was getting filed with multiples of the same attachment, and realized it was because the GMail API returns a different attachmentId for the same attachment, each time you request the message details.Ben H
Hmm, yes they don't seem to be stable and they really should be. The old and new ones should work but I agree that should get fixed.Eric D

2 Answers

1
votes

I have myself had to store attachment ids in database, and I am not even yet sure if both of my options below are indeed persistent. My tests over several weeks while working on it seems to suggest it however, but I cannot guarantee it.

  1. Use a key based on threadid, messageid and partid for the attachment.

  2. Take a look at the MessagePartHeader for an 'X-Attachment-Id' value. These id's is what I currently use for storing attachment ids.

1
votes

To others who may have this question, we were able to get to someone in Google who provided the following info:

  1. The attachmentID does not expire. The token is just generated from the stored remotePartReference for the attachment.
  2. If you are using the attachmentId, to avoid importing the same attachment twice to get a stable identifier for an attachment, you can try using a combination of file name + sha1 hash of the attachment. If you are calling Gmail APi via stubby, you should be able to read the sha1 hash of the attachment.

Hopefully others will also find this useful.