Gmail REST api batch support for getting messages

后端 未结 3 1644
深忆病人
深忆病人 2020-12-11 19:11

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

3条回答
  •  粉色の甜心
    2020-12-11 19:52

    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.

提交回复
热议问题