400 错误。需要收件人地址。JavaScript

Max*_*x S 3 google-apis-explorer gmail-api

我在 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')})
Run Code Online (Sandbox Code Playgroud)

但是收到一个错误:

{
   "error": {
   "errors": [
     {
       "domain": "global",
       "reason": "invalidArgument",
       "message": "Recipient address required"
     }
   ],
   "code": 400,
   "message": "Recipient address required"
  }
}
Run Code Online (Sandbox Code Playgroud)

noo*_*gui 6

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

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);
}
Run Code Online (Sandbox Code Playgroud)

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

[...] 需要在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". 
Run Code Online (Sandbox Code Playgroud)

因此,在对完整消息进行 base64 编码后,将其传递到raw参数中而不使用任何其他参数,它工作正常。