获取消息的 Gmail REST api 批处理支持 [英] Gmail REST api batch support for getting messages

查看:18
本文介绍了获取消息的 Gmail REST api 批处理支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要在我们的项目中从谷歌客户端库切换到 Gmail REST api,我遇到了 batch() 的问题,因为它在 REST api 中不可用 - 你只能获取消息的 id 列表,然后使用它的ID一一获取每条消息.如果我们使用那个 gmail 库,一切似乎都很清楚.我们创建一个批处理对象,然后在其中对每个 GET 请求进行排队.我们不必关心它是如何在内部实现的.目前我正在尝试做一些 POC,我正在测试这些建议 https://developers.google.com/gmail/api/guides/batch有邮递员,但没有运气..

We need to switch from google client library to Gmail REST api in our project, and I've encountered a problem with batch() because it is not available in REST api - you can only get list of ids of messages and then get each message one by one using it's id. If we use that gmail library everything seems to be clear. We create a batch object and then queue each GET request inside of it. We don't have to care how it's implemented inside. At the moment I'm trying to do some POC and I'm testing these suggestions https://developers.google.com/gmail/api/guides/batch with Postman but with no luck..

我收到了 400 个错误请求.Postman(或其他应用程序)中正确的请求正文应该如何?下一步将在 Java 中实现多部分请求并使用 RestTemplate 发送 POST,但我需要先在 Postman 中呈现一些 POC.

I'm getting 400 bad request. How should a proper request body look like in Postman (or other application)? The next step will be implementing multipart request in Java and sending POST using RestTemplate but I need to present some POC in Postman first.

我在这个屏幕截图上设置它 ->邮递员

I'm setting it like on this screenshot -> Postman

我做错了什么?:)

推荐答案

你很接近.这是一个工作示例:

You are close. Here is a working example:

请求

POST https://www.googleapis.com/batch
Content-Type: multipart/mixed; boundary="foo_bar"
Authorization: Bearer {ACCESS_TOKEN}

--foo_bar
Content-Type: application/http

GET /gmail/v1/users/me/messages/152d10540c21bd07

--foo_bar
Content-Type: application/http

GET /gmail/v1/users/me/messages/152d1050d666d7ad

--foo_bar--

回应

--batch_7Xp52oGIwpA_AAEAc7ERnGU
Content-Type: application/http

HTTP/1.1 200 OK
ETag: "A-DdBGA6g-wV4rIZCu5Hcm3JQpY/w2hzEg9kqXFH7AEJ-oSc-y10HNQ"
Content-Type: application/json; charset=UTF-8
Date: Thu, 11 Feb 2016 16:02:06 GMT
Expires: Thu, 11 Feb 2016 16:02:06 GMT
Cache-Control: private, max-age=0
Content-Length: 2809

{
 "id": "152d10540c21bd07",
 "threadId": "152d1050d666d7ad",
 "labelIds": [
  "SENT",
  "INBOX",
  "IMPORTANT"
 ],
 "snippet": "Likewise buddy.", ...
}

--batch_7Xp52oGIwpA_AAEAc7ERnGU
Content-Type: application/http

HTTP/1.1 200 OK
ETag: "A-DdBGA6g-wV4rIZCu5Hcm3JQpY/7v2nqQFBDmEHVvEQoboiwSidilE"
Content-Type: application/json; charset=UTF-8
Date: Thu, 11 Feb 2016 16:02:06 GMT
Expires: Thu, 11 Feb 2016 16:02:06 GMT
Cache-Control: private, max-age=0
Content-Length: 1752

{
 "id": "152d1050d666d7ad",
 "threadId": "152d1050d666d7ad",
 "labelIds": [
  "SENT",
  "INBOX",
  "IMPORTANT"
 ],
 "snippet": "Nice to meet you.", ...
}

--batch_7Xp52oGIwpA_AAEAc7ERnGU--

您不必在批处理的每个部分中指定主机,在 Authorization 标头中提供访问令牌就足够了.您不必自己指定 Content-Length,并且不要忘记用 " 包装边界字符串.

You don't have to specify the host in each part of the batch, and giving the access token in the Authorization header is enough. You don't have to specify the Content-Length yourself, and don't forget to wrap you boundary string with ".

然后你只需要解析每个部分的JSON就可以了.

Then you just have to parse the JSON of each part and you are done.

这篇关于获取消息的 Gmail REST api 批处理支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆