Invalid value for ByteString error when calling gmail send API with base64 encoded < or >

為{幸葍}努か 提交于 2019-11-30 00:36:31

问题


Really stuck on this one and pulling my hair out trying to figure out what to do, searched everywhere and tried everything but still stuck. Any help is much appreciated.

So I am calling the Gmail send message API:

https://www.googleapis.com/gmail/v1/users/me/messages/send

I pass in the following base64 encoded raw message:

VG86IGVjaG9zaWduLnNmZGMucWFAZ21haWwuY29tDQpTdWJqZWN0OiBSZTogd2UgaGF2ZSBhIGNvb2wgcHJvZHVjdA0KSW4tUmVwbHktVG86IDxDQUFMRnBBSmc1aTJlQ1p4T2dQWlhUdnNaN0J5M0gzNlRCMkNQYTdoVEtzTXh2bXZfT3dAbWFpbC5nbWFpbC5jb20+DQpSZWZlcmVuY2VzOiA8Q0FBTEZwQUpnNWkyZUNaeE9nUFpYVHZzWjdCeTNIMzZUQjJDUGE3aFRLc014dm12X093QG1haWwuZ21haWwuY29tPg0KDQoxMTozNg==

Which decodes to:

To: echosign.sfdc.qa@gmail.com
Subject: Re: we have a cool product
In-Reply-To: <CAALFpAJg5i2eCZxOgPZXTvsZ7By3H36TB2CPa7hTKsMxvmv_Ow@mail.gmail.com>
References: <CAALFpAJg5i2eCZxOgPZXTvsZ7By3H36TB2CPa7hTKsMxvmv_Ow@mail.gmail.com>

11:36

I get the error:

Status code: 400 and status: Bad Request and response: { "error": { "errors": [ { "domain": "global", "reason": "invalid", "message": "Invalid value for ByteString

The problem is the less than < and greater than > symbols in the In-Reply-To and References headers. I need those headers because I am trying to have the email come as a reply on the thread. I tried to leave out the < and > but the reply then does not thread.

Thanks


回答1:


If you are using PHP try this (otherwise try converting it to whatever language you are using)

Call the below function on your mime message before setting it to the raw property.

private function base64url_encode($mime) {
    return rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
}



回答2:


From Google's gmail rest api (sending messages):

"The entire email message in an RFC 2822 formatted and base64url encoded string. Returned in messages.get and drafts.get responses when the format=RAW parameter is supplied."

base64url encoding is not the same as base64 encoding.

in python, the base64 library offers b64encode() and urlsafe_b64encode(). If you don't use urlsafe_b64encode(), you will get that error, not all the time so it may pass a simple unit test, but it will fail soon enough.




回答3:


In Google Apps Script following fixes the issue

var draftBody = Utilities.base64EncodeWebSafe(raw);

Don't use var draftBody = Utilities.base64Encode(raw);




回答4:


The Google API decodes with urlsafe_b64decode. Try to use urlsafe_b64encode (encode strings using a URL-safe alphabet, which uses - instead of + and _ instead of / in the standard Base64 alphabet.)

In your example I see one + which means you used standard Base64.




回答5:


After you encode your message contents via base64Encode, like here (assuming google script):

var draftBody =  Utilities.base64Encode(message_content_string);

you have to convert all '/' and '+' characters that are not 'web-safe' and will produce the "Invalid value for ByteString" error. You can do it this way:

draftBody = draftBody.replace(/\//g,'_').replace(/\+/g,'-');

Then you submit it as a raw data. Apparently there is no distortion of message contents here.



来源:https://stackoverflow.com/questions/26663529/invalid-value-for-bytestring-error-when-calling-gmail-send-api-with-base64-encod

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!