Search code examples
gmail-apigoogle-api-cpp-client

GMail api c++ sample


Are there any samples available on how to use the GMAIL API to read messages for c++?

I have the Google CPP Client and have got the calendar sample running. Wondering if there is a similar Gmail Sample.


Solution

  • I'm not aware of samples and dont see a generated API for it so you'd need to do this yourself. It isnt all that hard to do yourself but, unfortunately, can get tedious if you need the full breadth of API.

    You'll need to talk HTTP directly to gmail using it's REST interface https://developers.google.com/gmail/api/

    You can do this however you want. Using the Google APIs for C++ library you can either use the HTTP Transport layer directly or you can build on top of the Client Service libraries abstract classes. The Client Service library classes were designed to be targeted by a code generator, but is still probably reasonable as a starting point because the base class already does all the interesting stuff so you just need to feed it with the tedious details specific to the GMail API. Which one I'd pick depends on how much breadth of API I need and if I'm using other APIs as well where consistency and generalization across your codebase might come into play.

    You can use the Calendar API you are already familiar with as an example. Keep in mind that this is a complete coverage of the API, which you wouldnt need at first so might want to make some simplifying shortcuts as you get started (e.g. implement one method with limited parameter support).

    You want to look at the service adaption as implemented in:

    https://github.com/google/google-api-cpp-client/blob/master/service_apis/calendar/google/calendar_api/calendar_service.h

    and

    https://github.com/google/google-api-cpp-client/blob/master/service_apis/calendar/google/calendar_api/calendar_service.cc

    Those files are partitioned into the different operations on the different resource types within Calendar. Just pick one method type (e.g. CalendarListResource_ListMethod) and look at the Calendar REST API for it (https://developers.google.com/google-apps/calendar/v3/reference/#CalendarList) to follow along. Then use that as an analogy to a corresponding Gmail REST API (I'm guessing https://developers.google.com/gmail/api/v1/reference/users/messages)

    Note that you dont need to specialize the data objects for the API (which is most of the classes generated) and you can take some shortcuts in those method object implementations if you are not implementing the whole API at first. But maybe follow the pattern for the generated code until you are comfortable so that you are less likely to introduce inconsistencies that break the base class.

    In the end "all you are doing" is sending HTTP messages using a REST style pattern, and grabbing responses. The base class has all the boilerplate heuristics and protocol/encodings already implemented but needs to you to provide the specific URLs, payloads, and occasional header then interpret the HTTP response bodies for the semantics of the API you are calling.