【问题标题】:Node.js POST request to Gmail API Send Message向 Gmail API 发送消息的 Node.js POST 请求
【发布时间】:2018-02-17 18:02:15
【问题描述】:

我正在尝试使用服务器端 node.js 向 Gmail 发送消息 API 发送请求,但没有成功。我收到以下错误:

 body: '{
 "error": {
  "errors": [
   {
    "domain": "global", 
    "reason": "invalidArgument",
    "message": "\'raw\' RFC822 payload
     message string or uploading message via /upload/ URL required"  
   }
  ],
  "code": 400,
  "message": "\'raw\' RFC822 payload message
   string or uploading message via /upload/ URL required"
 }
}'
}

oauth2token 和 raw 的输入参数是有效的,事实上,如果我使用 Google OAuth 2 Playground(https://developers.google.com/oauthplayground) 并使用 token 和 raw 作为电子邮件发送的值。有人能看到我错过了什么吗?

    function sendMail(oauth2token, raw) {
    context.log('Token: ' + oauth2token);
    context.log('raw: ' + raw);

    var params = {
        userId: user_id,
        resource: { 'raw': raw}
    };

    var headers = {
        "HTTP-Version": "HTTP/1.1",
        "Content-Type": "application/json",
        "Authorization": "Bearer " + oauth2token
    };

    var options = {
        headers: headers,
        url: "https://www.googleapis.com/gmail/v1/users/me/messages/send",
        method: "POST",
        params: params
    };

    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            context.log(body);
        }
        if (error) {
            context.log(error);
        }
        else {
            context.log(response);
        }
    })
}

【问题讨论】:

  • 我也有同样的问题。你解决过这个问题吗?

标签: node.js request gmail gmail-api


【解决方案1】:

如果您在 Google Playground 中进行测试并且一切看起来都不错,我会开始查看您正在使用的其他外部依赖项。例如,请求。也许您需要传入解析的 url 对象而不是 url。看看这个:https://github.com/request/request#requestoptions-callback

这是您解析的 url 对象的样子:

Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.googleapis.com',
  port: null,
  hostname: 'www.googleapis.com',
  hash: null,
  search: null,
  query: null,
  pathname: '/gmail/v1/users/me/messages/send',
  path: '/gmail/v1/users/me/messages/send',
  href: 'https://www.googleapis.com/gmail/v1/users/me/messages/send' }

或者将现有选项更改为 uri 而不是 url

【讨论】:

    【解决方案2】:

    你只需要在body中传递raw消息:

    function sendMail (oauth2token, raw) {
      var options = {
        method: 'POST',
        url: 'https://www.googleapis.com/gmail/v1/users/me/messages/send',
        headers: {
          'HTTP-Version': 'HTTP/1.1',
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + oauth2token
        },
        body: JSON.stringify({
          raw: raw
        })
      };
    
      request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
          context.log(body);
        }
        if (error) {
          context.log(error);
        } else {
          context.log(response);
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 2016-05-12
      • 2015-01-26
      • 2012-08-31
      • 2019-06-01
      • 2016-04-14
      • 2017-03-03
      • 1970-01-01
      相关资源
      最近更新 更多