Search code examples
gmailgoogle-oauthgmail-api

How to not get "snippet" field in Gmail Message responses


I can not figure out how to tell the Gmail API to not return the Gmail Message "snippet" field.

I am making my users.messages GET request with the following parameters:

  const getOptions: gmail_v1.Params$Resource$Users$Messages$Get = {
    userId: 'me',
    access_token: user.gmailAccessToken, // access token with the https://www.googleapis.com/auth/gmail.modify scope
    format: GmailMessageFormat.METADATA, // no need to get full message, just get necessary metadata for each message
    metadataHeaders: ['From', 'Message-ID', 'To'],
    id: messageId
  };

As one can see I am only requesting GmailMessageFormat.METADATA as the format and even then only asking for three fields, yet still, in the response bodies, I am getting the "snippet" attribute fully populated with, "a short part of the message text", which I consider highly sensitive and do not want.

Is there any way to not get this sensitive field in the response body while still getting select headers like "to", "from" and "message-id"?

As one can see I am actually using the nodejs sdk, not the rest api, but ultimately they are the same thing. Here is the nodejs users.messages.get documentation in case anyone is curious.

Thanks for the assistance!


Solution

  • I'm writing this answer as a community wiki, since the issue was resolved from the comments section, in order to provide a proper response to the question.

    Solution:

    As stated in a comment, only the https://www.googleapis.com/auth/gmail.modify scope was being used when doing the API call which would end up with a similar result to:

    enter image description here

    The result contains a snippet of the message which is not desired, however when adding the proper scope which is https://www.googleapis.com/auth/gmail.metadata, this would end up in the following result:

    enter image description here

    This last result does not include the snippet as the correct scope was being used this time. This is an expected behavior since the metadata scope is the one that can return the metadata of a message without any body content or attachments, according to Google's documentation.

    Read resources metadata including labels, history records, and email message headers, but not the message body or attachments.

    References: