Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmail REST api batch support for getting messages

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

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

What am I doing wrong?:)

like image 887
dune76 Avatar asked Feb 11 '16 15:02

dune76


People also ask

How do I automate Gmail API?

Enable Gmail API So you need to go to https://console.developers.google.com to activate the API access of the email that you will use in your project. Click ENABLE APIS AND SERVICES. It will take you to the search page. There, you need to search for GMAIL.

Can we call API in batch?

Batch calls allow API applications to make multiple API calls within a single API call. In addition, each call can be tied to a different access_token meaning batch API calls work with multiple users. Batch calls allow your platform to accomplish more API interaction with less traffic, thus avoiding throttle limits.

What is REST API batch?

The batch mode allows creating and / or updating multiple elements in one request. Notice the list of resources which supports the batch mode. The results of the different tasks (create / update) is stacked and returns one result. In addition, the batch mode supports the detaching of elements in on request.

How do I send a batch request?

Format of a batch request A batch request is a single standard HTTP request containing multiple Admin SDK API calls, using the multipart/mixed content type. Within that main HTTP request, each of the parts contains a nested HTTP request. Each part begins with its own Content-Type: application/http HTTP header.


3 Answers

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

Response

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

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

like image 88
Tholle Avatar answered Nov 15 '22 11:11

Tholle


  • You need to include gmail/v1 on the POST URL and on each request.
  • Don't forget about the " around your boundary on the Content-Type header.

See original batch gmail requests documentation: https://developers.google.com/gmail/api/guides/batch

The following worked for me:

POST /batch/gmail/v1 HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer {YOUR_ACCESS_TOKEN}
Content-Type: multipart/mixed; boundary="foo_bar"

--foo_bar
Content-Type: application/http

GET /gmail/v1/users/me/messages/1732ebdcb9b8cccf
--foo_bar
Content-Type: application/http

GET /gmail/v1/users/me/messages/1732ecadae4907e2

--foo_bar--

Creating request with Postman - body screenshot

Creating request with Postman - headers screenshot

like image 39
Lucila Avatar answered Nov 15 '22 09:11

Lucila


Just wanted to say that Lucila's answer is now the correct one - the global (https://www.googleapis.com/batch) endpoint is now deprecated, and you must make a post request to your request-specific endpoint (https://www.googleapis.com/batch/gmail/v1 for gmail, for instance).

See this link for additional context.

Apologies for making a new answer for this, I don't have enough reputation to leave a comment.

like image 38
Utkarsh Dalal Avatar answered Nov 15 '22 11:11

Utkarsh Dalal