【问题标题】:400 error. Recipient address required. JavaScript400 错误。需要收件人地址。 JavaScript
【发布时间】:2018-08-14 18:47:51
【问题描述】:

我在 Gmail API 中逐步提出一个简单的请求。

根据所有指示,我打了这个电话:

var request = gapi.client.gmail.users.messages.send({
    'userId': 'me',
    "payload": {
    "headers": [
             {
              "name": "To",
              "value": "########@gmail.com"
             }
           ]},
     'raw': 'SEVMTE8gTVkgREVBUiBGUklFTkQ='
     });

request.then(()=>{console.log('yep')})

但是收到错误:

{
   "error": {
   "errors": [
     {
       "domain": "global",
       "reason": "invalidArgument",
       "message": "Recipient address required"
     }
   ],
   "code": 400,
   "message": "Recipient address required"
  }
}

【问题讨论】:

    标签: gmail-api google-apis-explorer


    【解决方案1】:

    Users.messages: send 中说明了在 Gmail API 中发送电子邮件的正确格式:

    function sendMessage(userId, email, callback) {
      // Using the js-base64 library for encoding:
      // https://www.npmjs.com/package/js-base64
      var base64EncodedEmail = Base64.encodeURI(email);
      var request = gapi.client.gmail.users.messages.send({
        'userId': userId,
        'resource': {
          'raw': base64EncodedEmail
        }
      });
      request.execute(callback);
    }
    

    有关如何使用此方法的更生动示例,请查看此SO post

    [...]完整的消息需要在raw参数中传递,见例子:

    From: John Doe <jdoe@machine.example> 
    To: Mary Smith <mary@example.net> 
    Subject: Saying Hello 
    Date: Fri, 21 Nov 1997 09:55:06 -0600 
    Message-ID: <1234@local.machine.example>
    
    This is a message just to say hello. So, "Hello". 
    

    所以在对完整消息进行base64编码后,将其传递给raw参数而不使用任何其他参数,它就可以正常工作了。

    【讨论】:

    • 将整个信息编码为 base64 以解决发送问题。万分感谢。你能帮我发一封html信吗?
    • 我已经知道如何发送 html 信件了。添加 Content-Type: text/html; charset=utf-8 变成了一个决议。再次感谢您/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2019-09-21
    • 2019-10-30
    • 2020-08-23
    • 2017-04-30
    • 2015-10-31
    • 1970-01-01
    相关资源
    最近更新 更多