Gmail support REST api for receiving messages

We need to switch from the Google client library to the Gmail REST api in our project, and I encountered a problem with the package () because it is not available in the REST api - you can only get a list of message identifiers, and then receive each message one at a time, using his id. If we use this gmail library, everything seems clear. We create a batch object and then queue every GET request inside it. We do not need to care about how this is implemented internally. Right now I'm trying to do some POC, and I'm testing these offers https://developers.google.com/gmail/api/guides/batch with a postman, but no luck.

I get 400 bad requests. What should the request authority look like in Postman (or another application)? The next step is to implement the multipart request in Java and send the POST using RestTemplate, but I need to submit the POC to Postman first.

I install it as in this screenshot → Postman

What am I doing wrong?:)

+2
source share
1 answer

You are close. Here is a working example:

Request

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

answer

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

You do not need to specify a host in each part of the package, and just specify the access token in the authorization header. You do not need to specify Content-Length yourself, and remember to wrap the boundary string with ".

Then you just need to parse the JSON of each part, and you're done.

+5

Source: https://habr.com/ru/post/1628517/


All Articles