Ben H
Ben H

Reputation: 133

How can I find the definitive attachmentId for an attachment retrieved via Google's Gmail API?

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?

Upvotes: 11

Views: 3018

Answers (2)

Ron Ih
Ron Ih

Reputation: 46

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.

Upvotes: 3

Geir Thomas Jakobsen
Geir Thomas Jakobsen

Reputation: 111

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.

Upvotes: 1

Related Questions