400 error. Recipient address required. JavaScript

喜欢而已 提交于 2019-12-11 13:36:24

问题


I make a simple request step-by-step in Gmail API.

According to all instructions I made this call:

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

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

But receive an error:

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

回答1:


The correct format in sending to emails in Gmail API is stated in 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);
}

For a more vivid sample on how to use this method, check this SO post:

[...] the complete message needs to be passed in the raw parameter, see the example:

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". 

So after base64 encoding the complete message, passing it in the raw parameter without using any other parameter, it works fine.



来源:https://stackoverflow.com/questions/49138836/400-error-recipient-address-required-javascript

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