0

How do I get the full message and not just the metadata using gmail api?

I have a service account and I am able to retrieve a message but only in the metadata, raw and minimal formats. How do I retrieve the full message in the full format? The following code works fine

var request = service.Users.Messages.Get(userId, messageId);
request.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Metadata;
Message message = request.Execute();

However, when I omit the format (hence I use the default format which is FULL) or I change the format to UsersResource.MessagesResource.GetRequest.FormatEnum.Full I get the error: Metadata scope doesn't allow format FULL

I have included the following scopes:

https://www.googleapis.com/auth/gmail.readonly, 
https://www.googleapis.com/auth/gmail.metadata,
https://www.googleapis.com/auth/gmail.modify,
https://mail.google.com/

How do I get the full message?

jpo
  • 3,959
  • 20
  • 59
  • 102

3 Answers3

3

I had to remove the scope for the metadata to be able to get the full message format.

jpo
  • 3,959
  • 20
  • 59
  • 102
  • 1
    yeah - curious .. I had thought that the most permissive scope would override the lesser but this would appear to not be the case. – Peter Scott Sep 30 '18 at 20:21
1

The user from the SO post have the same error.

Try this out first.

  1. Go to https://security.google.com/settings/security/permissions
  2. Choose the app you are working with.
  3. Click Remove > OK
  4. Next time, just request exactly which permissions you need.

Another thing, try to use gmailMessage.payload.parts[0].body.dataand to decode it into readable text, do the following from the SO post:

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;

System.out.println(StringUtils.newStringUtf8(Base64.decodeBase64(gmailMessage.payload.parts[0].body.data)));

You can also check this for further reference.

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65
  • I removed the app (The Google API explorer) and tried again, but same error. What does point 4 mean(Next time, just request exactly which permissions you need)? Am I supposed to edit my scopes? I am able to execute the request so I am not able to get to the gmailMessage.payload.parts[0].body.data – jpo Aug 10 '17 at 12:03
0

try something like this

    public String getMessage(string user_id, string message_id)
    {
         Message temp =service.Users.Messages.Get(user_id,message_id).Execute();
         var parts = temp.Payload.Parts;
         string s = "";
         foreach (var part in parts) {
                byte[] data = FromBase64ForUrlString(part.Body.Data);
                s += Encoding.UTF8.GetString(data);
         }
        return s
       }

    public static byte[] FromBase64ForUrlString(string base64ForUrlInput)
        {
            int padChars = (base64ForUrlInput.Length % 4) == 0 ? 0 : (4 - (base64ForUrlInput.Length % 4));
            StringBuilder result = new StringBuilder(base64ForUrlInput, base64ForUrlInput.Length + padChars);
            result.Append(String.Empty.PadRight(padChars, '='));
            result.Replace('-', '+');
            result.Replace('_', '/');
            return Convert.FromBase64String(result.ToString());
        }
  • 1
    I get the error "Metadata scope doesn't allow format FULL". Full is the default format so when I do not specify the format, I get this error. – jpo Aug 15 '17 at 14:04