C++ Program to List Events & Holidays in Calendar Using Google Calendar API Full Project For Beginners

  • Post author:
  • Post category:c++
  • Post comments:0 Comments

 

 

calendar.cpp

 

 

#include <CkRest.h>
#include <CkOAuth2.h>
#include <CkStringBuilder.h>
#include <CkJsonObject.h>

void ChilkatSample(void)
    {
    CkRest rest;
    bool success;

    //   Provide a previously obtained OAuth2 access token.
    CkOAuth2 oauth2;
    oauth2.put_AccessToken("OAUTH2_ACCESS_TOKEN");
    rest.SetAuthOAuth2(oauth2);

    success = rest.Connect("www.googleapis.com",443,true,true);
    if (success != true) {
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    CkStringBuilder sbJson;
    success = rest.FullRequestNoBodySb("GET","/calendar/v3/calendars/support@chilkatcloud.com/events",sbJson);
    if (success != true) {
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    if (rest.get_ResponseStatusCode() != 200) {
        std::cout << "Received error response code: " << rest.get_ResponseStatusCode() << "\r\n";
        std::cout << "Response body:" << "\r\n";
        std::cout << sbJson.getAsString() << "\r\n";
        return;
    }

    CkJsonObject json;
    json.LoadSb(sbJson);

    //  The following code parses the JSON response.
    //  A sample JSON response is shown below the sample code.
    const char *kind = 0;
    const char *etag = 0;
    const char *summary = 0;
    const char *updated = 0;
    const char *timeZone = 0;
    const char *accessRole = 0;
    const char *nextSyncToken = 0;
    int i;
    int count_i;
    const char *method = 0;
    int minutes;
    const char *id = 0;
    const char *status = 0;
    const char *htmlLink = 0;
    const char *created = 0;
    const char *creatorEmail = 0;
    bool creatorSelf;
    const char *organizerEmail = 0;
    bool organizerSelf;
    const char *startDateTime = 0;
    const char *endDateTime = 0;
    const char *iCalUID = 0;
    int sequence;
    const char *hangoutLink = 0;
    bool remindersUseDefault;
    const char *location = 0;

    kind = json.stringOf("kind");
    etag = json.stringOf("etag");
    summary = json.stringOf("summary");
    updated = json.stringOf("updated");
    timeZone = json.stringOf("timeZone");
    accessRole = json.stringOf("accessRole");
    nextSyncToken = json.stringOf("nextSyncToken");
    i = 0;
    count_i = json.SizeOfArray("defaultReminders");
    while (i < count_i) {
        json.put_I(i);
        method = json.stringOf("defaultReminders[i].method");
        minutes = json.IntOf("defaultReminders[i].minutes");
        i = i + 1;
    }

    i = 0;
    count_i = json.SizeOfArray("items");
    while (i < count_i) {
        json.put_I(i);
        kind = json.stringOf("items[i].kind");
        etag = json.stringOf("items[i].etag");
        id = json.stringOf("items[i].id");
        status = json.stringOf("items[i].status");
        htmlLink = json.stringOf("items[i].htmlLink");
        created = json.stringOf("items[i].created");
        updated = json.stringOf("items[i].updated");
        summary = json.stringOf("items[i].summary");
        creatorEmail = json.stringOf("items[i].creator.email");
        creatorSelf = json.BoolOf("items[i].creator.self");
        organizerEmail = json.stringOf("items[i].organizer.email");
        organizerSelf = json.BoolOf("items[i].organizer.self");
        startDateTime = json.stringOf("items[i].start.dateTime");
        endDateTime = json.stringOf("items[i].end.dateTime");
        iCalUID = json.stringOf("items[i].iCalUID");
        sequence = json.IntOf("items[i].sequence");
        hangoutLink = json.stringOf("items[i].hangoutLink");
        remindersUseDefault = json.BoolOf("items[i].reminders.useDefault");
        location = json.stringOf("items[i].location");
        i = i + 1;
    }

    std::cout << "Example Completed." << "\r\n";
    }

Leave a Reply