使用Google GMail游乐场时,为什么我会一直收到“400无效标题”回复?

问题描述 投票:1回答:2

我花了两天时间试图通过Google OAuth playground发送电子邮件,但没有运气。这是我要发送的原始邮件:

To: "Stanley Smith" <[email protected]>\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编码),然后将编码的字符串放在请求体中

{
    "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所以我不知道为什么我会收到此错误。为什么我收到此错误?

gmail gmail-api
2个回答
3
投票

我不完全确定你为什么会得到这个错误。如果您重新排列标头并将其编码为base64 url​​-safe,则它可以正常工作:

btoa(
  "From: \"Stanley Toles\" <[email protected]>\r\n" +
  "To: \"Stanley Toles\" <[email protected]>\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(/=+$/, '');

1
投票

这篇文章给了我很多帮助。谢谢你问问Rob。

最初,我使用以下代码来收发电子邮件:

let email = {message: "To: firstname lastname<[email protected]>  From: firstname 
lastname <[email protected]> Subject: Saying Hello  Date: Tue, 9 Nov 2018 17:20:06 
-0600 
Message-ID: <[email protected]>  This is a message just to say hello."}

看完你的帖子后,我意识到我错过了返回角色。以下是我在javascript中使用google gmail api发送电子邮件的最终答案:

function sendMessage(auth) {

    let email = {message: `To: "first last" <[email protected]>\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 <[email protected]>"},
       {name: 'From', value: 'first last <[email protected]>'},
       {name: 'Subject', value: 'Saying Hello'}]
    },
    'raw': base64EncodedEmail,


    }},function(err,response){

    if(err) throw err;

    console.log(response);
   });
  }
© www.soinside.com 2019 - 2024. All rights reserved.