Google-api-ruby-client: Gmail API - message raw field decoding error

Created on 18 Aug 2014  路  7Comments  路  Source: googleapis/google-api-ruby-client

when calling https://developers.google.com/gmail/api/v1/reference/users/messages/get with format=raw the raw field is decoded wrong. For example, result['raw'] appears to be decoded with Base64.decode64 but this is incorrect and it should use Base64.urlsafe_decode64. To fix this I have to convert to JSON, parse it, then urlsafe_decode64 myself.

message_data = result.data
message_json = JSON.parse(message_data.to_json())
mime_data = Base64.urlsafe_decode64(message_json['raw'])

p2 bug

Most helpful comment

Here is the solution:
Gmail API - "Users.messages: get" method has in response message.payload.body.data parted base64 data, it's separated by "-" symbol. It's not entire base64 encoded text, it's parts of base64 text. You have to try to decode every single part of this or make one mono string by unite and replace "-" symbol.

All 7 comments

!!! Oh thank you so so so so so much. I never would have figured this out myself

me too. Thank you @dgobaud !

Had the same problem when getting messages with format=full.
Converting the results data to JSON and manually decoding worked.

message_data = response.data.payload.parts.first
json_data = JSON.parse(message_data.to_json)
decoded_message = Base64.urlsafe_decode64(json_data["body"]["data"])

Thanks @dgobaud !

Fixed in 0.9 which uses urlsafe decoding/encoding throughout

Here is the solution:
Gmail API - "Users.messages: get" method has in response message.payload.body.data parted base64 data, it's separated by "-" symbol. It's not entire base64 encoded text, it's parts of base64 text. You have to try to decode every single part of this or make one mono string by unite and replace "-" symbol.

Bro, you saved my day! I spent my whole day trying to fix base64 padding-errors!

Had the same problem when getting messages with format=full.
Converting the results data to JSON and manually decoding worked.

message_data = response.data.payload.parts.first
json_data = JSON.parse(message_data.to_json)
decoded_message = Base64.urlsafe_decode64(json_data["body"]["data"])

Thanks @dgobaud !

How can i do this in flutter for getting gmail messages?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidraj picture davidraj  路  6Comments

a14m picture a14m  路  7Comments

asheren picture asheren  路  5Comments

brandonhilkert picture brandonhilkert  路  6Comments

bootstraponline picture bootstraponline  路  4Comments