Gmail API decoding messages in Javascript

后端 未结 7 1011
暖寄归人
暖寄归人 2020-12-30 11:07

I am having serious problems decoding the message body of the emails I get using the Gmail API. I want to grab the message content and put the content in a div. I am using a

7条回答
  •  清酒与你
    2020-12-30 11:33

    Thank @ento 's answer. I explain more why you need to replace '-' and '_' character to '+' and '/' before decode.

    Wiki Base64 Variants summary table shows:

    • RFC 4648 section 4: base64 (standard): use '+' and '/'
    • RFC 4648 section 5: base64url (URL-safe and filename-safe standard): use '-' and '_'

    In short, Gmail API use base64url (urlsafe) format('-' and '_'), But JavaScript atob function or other JavaScript libraries use base64 (standard) format('+' and '/').

    For Gmail API, the document says body use base64url format, see below links:

    • string/bytes type
    • MessagePartBody
    • RAW

    For Web atob/btoa standards, see below links:

    • The algorithm used by atob() and btoa() is specified in RFC 4648, section 4
    • 8.3 Base64 utility methods
    • Forgiving base64

提交回复
热议问题