【问题标题】:Gmail API returns 400 BadRequest with batch requestGmail API 使用批处理请求返回 400 BadRequest
【发布时间】:2016-03-15 14:54:14
【问题描述】:

我正在尝试使用以下内容向 gmail 发出批处理 http 请求(在使用 Meteor/HTTP 的服务器上):

batchGetMessages = (accessToken, ids) => {
  let userId = 'me';
  let url = `https://www.googleapis.com/batch`;
  let boundary = `batch_message_request`;
  let body = ``;

  _.each(ids, (id) => {
    body = `${body}
    --${boundary}
    Content-Type: application/http

    GET /gmail/v1/users/${userId}/messages/${id.id}?format=metadata&fields=id%2Cpayload
    `
  });

  body = `${body}
    --${boundary}--`

  let options = {
    headers: {
      'Authorization': `Bearer ${accessToken}`,
      'Content-Type': `multipart/mixed; boundary="${boundary}"`,
    },
    content: body,
  };

  let data = HTTP.post(url, options);
  console.log('data: ', data);
}

身体的琴弦看起来像这样:

--batch_message_request
Content-Type: application/http

GET /gmail/v1/users/me/messages/15375be281102d3d?format=metadata&fields=id%2Cpayload

--batch_message_request
Content-Type: application/http

GET /gmail/v1/users/me/messages/15366f87db6bdfeb?format=metadata&fields=id%2Cpayload

--batch_message_request
Content-Type: application/http

GET /gmail/v1/users/me/messages/15365d62f152dea2?format=metadata&fields=id%2Cpayload

--batch_message_request--

我的请求总是返回 400 Bad Request 错误。我已经检查了类似的问题,但还没有得到这个工作:

Generating HTTP multipart body for file upload in JavaScript

Gmail REST api batch support for getting messages

Batch request - 400 bad request response

任何帮助或建议将不胜感激。谢谢!

【问题讨论】:

    标签: javascript meteor google-api


    【解决方案1】:

    嗯,400 BadRequest 错误似乎是由字符串模板中的换行符引起的。删除空格让一切正常:

    ...
    /* 
      the lack of whitespace is impotant in the folllowing string template:
    */
      _.each(ids, (id) => {
        body = `${body}
    --${boundary}
    Content-Type: application/http
    
    GET /gmail/v1/users/${userId}/messages/${id.id}? format=metadata&fields=id%2Cpayload`
      });
    
      body = `${body}
    --${boundary}--`;
    ...
    

    可能有更好的方法来维护代码缩进并消除空格,但我还没有找到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 2020-01-20
      • 2015-12-18
      相关资源
      最近更新 更多