为什么我在使用 Google GMail playground 时总是收到“400 invalid to header”响应?
Why do I keep getting a "400 invalid to header" response when using the Google GMail playground?
我花了两天时间尝试通过 Google OAuth playground 发送电子邮件,但没有成功。这是我要发送的原始消息:
To: "Stanley Smith" <stan.smith@yahoo.com>\r\nContent-type: text/html;charset=iso-8859-1\r\nMIME-Version: 1.0\r\nSubject: this would be the subject\r\n\r\nThis is the email sent by Stanley Smith
我对其进行 base64 编码(url-safe 编码),然后将编码后的字符串原样放入请求 body
{
"raw": "VG86ICJTdGFubGV5IFNtaXRoIiA8c3Rhbi5zbWl0aEB5YWhvby5jb20+XHJcbkNvbnRlbnQtdHlwZTogdGV4dC9odG1sO2NoYXJzZXQ9aXNvLTg4NTktMVxyXG5NSU1FLVZlcnNpb246IDEuMFxyXG5TdWJqZWN0OiB0aGlzIHdvdWxkIGJlIHRoZSBzdWJqZWN0XHJcblxyXG5UaGlzIGlzIHRoZSBlbWFpbCBzZW50IGJ5IFN0YW5sZXkgU21pdGgK"
}
然后我点击发送请求,但我一直收到这个错误:
HTTP/1.1 400 Bad Request
Content-length: 188
X-xss-protection: 1; mode=block
X-content-type-options: nosniff
Expires: Fri, 16 Dec 2016 15:27:27 GMT
Vary: Origin,X-Origin
Server: GSE
Cache-control: private, max-age=0
Date: Fri, 16 Dec 2016 15:27:27 GMT
X-frame-options: SAMEORIGIN
Content-type: application/json; charset=UTF-8
{
"error": {
"code": 400,
"message": "Invalid to header",
"errors": [
{
"domain": "global",
"message": "Invalid to header",
"reason": "invalidArgument"
}
]
}
}
我遵循 RFC 2822,所以我不知道为什么会收到此错误。为什么会出现此错误?
我不完全确定为什么会出现此错误。如果您重新排列 headers 并将其编码为 base64 url-safe:
,它就会起作用
btoa(
"From: \"Stanley Toles\" <stan.toles@yahoo.com>\r\n" +
"To: \"Stanley Toles\" <stan.toles@yahoo.com>\r\n" +
"Subject: this would be the subject\r\n" +
"Content-type: text/html;charset=iso-8859-1\r\n\r\n" +
"This is the email sent by Stanley Toles"
).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
这个post对我帮助很大。感谢您提出问题 Rob。
最初,我在电子邮件中使用了以下代码:
let email = {message: "To: firstname lastname<someperson@gmail.com> From: firstname
lastname <someperson@gmail.com> Subject: Saying Hello Date: Tue, 9 Nov 2018 17:20:06
-0600
Message-ID: <1234@local.machine.example> This is a message just to say hello."}
阅读您的 post 后,我意识到我遗漏了 return 个字符。这是我在 javascript:
中使用 google gmail api 发送电子邮件的最终答案
function sendMessage(auth) {
let email = {message: `To: "first last" <someperson@gmail.com>\r\nContent-type:
text/html;charset=iso-8859-1\r\nMIME-Version: 1.0\r\nSubject: this would be the
subject\r\n\r\nThis is the email sent by Stanley Toles`}
let base64EncodedEmail = Base64.encodeURI(email.message);
gmail.users.messages.send({'auth': auth, 'userId': 'me', 'resource': {
"payload": {
"headers": [
{name: "To", value: "first last <somepersonsemail@gmail.com>"},
{name: 'From', value: 'first last <somepersonsemail@gmail.com>'},
{name: 'Subject', value: 'Saying Hello'}]
},
'raw': base64EncodedEmail,
}},function(err,response){
if(err) throw err;
console.log(response);
});
}
我花了两天时间尝试通过 Google OAuth playground 发送电子邮件,但没有成功。这是我要发送的原始消息:
To: "Stanley Smith" <stan.smith@yahoo.com>\r\nContent-type: text/html;charset=iso-8859-1\r\nMIME-Version: 1.0\r\nSubject: this would be the subject\r\n\r\nThis is the email sent by Stanley Smith
我对其进行 base64 编码(url-safe 编码),然后将编码后的字符串原样放入请求 body
{
"raw": "VG86ICJTdGFubGV5IFNtaXRoIiA8c3Rhbi5zbWl0aEB5YWhvby5jb20+XHJcbkNvbnRlbnQtdHlwZTogdGV4dC9odG1sO2NoYXJzZXQ9aXNvLTg4NTktMVxyXG5NSU1FLVZlcnNpb246IDEuMFxyXG5TdWJqZWN0OiB0aGlzIHdvdWxkIGJlIHRoZSBzdWJqZWN0XHJcblxyXG5UaGlzIGlzIHRoZSBlbWFpbCBzZW50IGJ5IFN0YW5sZXkgU21pdGgK"
}
然后我点击发送请求,但我一直收到这个错误:
HTTP/1.1 400 Bad Request
Content-length: 188
X-xss-protection: 1; mode=block
X-content-type-options: nosniff
Expires: Fri, 16 Dec 2016 15:27:27 GMT
Vary: Origin,X-Origin
Server: GSE
Cache-control: private, max-age=0
Date: Fri, 16 Dec 2016 15:27:27 GMT
X-frame-options: SAMEORIGIN
Content-type: application/json; charset=UTF-8
{
"error": {
"code": 400,
"message": "Invalid to header",
"errors": [
{
"domain": "global",
"message": "Invalid to header",
"reason": "invalidArgument"
}
]
}
}
我遵循 RFC 2822,所以我不知道为什么会收到此错误。为什么会出现此错误?
我不完全确定为什么会出现此错误。如果您重新排列 headers 并将其编码为 base64 url-safe:
,它就会起作用btoa(
"From: \"Stanley Toles\" <stan.toles@yahoo.com>\r\n" +
"To: \"Stanley Toles\" <stan.toles@yahoo.com>\r\n" +
"Subject: this would be the subject\r\n" +
"Content-type: text/html;charset=iso-8859-1\r\n\r\n" +
"This is the email sent by Stanley Toles"
).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
这个post对我帮助很大。感谢您提出问题 Rob。
最初,我在电子邮件中使用了以下代码:
let email = {message: "To: firstname lastname<someperson@gmail.com> From: firstname
lastname <someperson@gmail.com> Subject: Saying Hello Date: Tue, 9 Nov 2018 17:20:06
-0600
Message-ID: <1234@local.machine.example> This is a message just to say hello."}
阅读您的 post 后,我意识到我遗漏了 return 个字符。这是我在 javascript:
中使用 google gmail api 发送电子邮件的最终答案function sendMessage(auth) {
let email = {message: `To: "first last" <someperson@gmail.com>\r\nContent-type:
text/html;charset=iso-8859-1\r\nMIME-Version: 1.0\r\nSubject: this would be the
subject\r\n\r\nThis is the email sent by Stanley Toles`}
let base64EncodedEmail = Base64.encodeURI(email.message);
gmail.users.messages.send({'auth': auth, 'userId': 'me', 'resource': {
"payload": {
"headers": [
{name: "To", value: "first last <somepersonsemail@gmail.com>"},
{name: 'From', value: 'first last <somepersonsemail@gmail.com>'},
{name: 'Subject', value: 'Saying Hello'}]
},
'raw': base64EncodedEmail,
}},function(err,response){
if(err) throw err;
console.log(response);
});
}