Search code examples
google-apigmail-api

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?


Solution

  • 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.