0

I have my Google calendar API working well with the following code:

private $client;


    public function __construct() {


        $this->client = new Google_Client();
        $this->client->setAuthConfig(__DIR__ . '/client_id.json');
        $this->client->setScopes("https://www.googleapis.com/auth/calendar.events.readonly");
        $calendarService = new Google_Service_Calendar($this->client);

    }


public function showCalendar() {
    $calendarService = new Google_Service_Calendar($this->client);
    $myCalendarID = "en.usa#holiday@group.v.calendar.google.com";
    $events = $calendarService->events->listEvents($myCalendarID, array(
                                'timeMin' => date(DATE_RFC3339), 
                                'maxResults' => 4))->getItems();


    foreach ($events as $e) {
        $this->eventsToReturn .= "<div class='summary'>";
        $this->eventsToReturn .= $e->getSummary();
        $this->eventsToReturn .= ' <span>(';
        $this->eventsToReturn .= $e->start->date;
        $this->eventsToReturn .= ' )<span>';
        $this->eventsToReturn .= "</div>";


    }
    return $this->eventsToReturn;

The code above is using a public calendar. As soon as I change $myCalendarID to reference a private calendar, I get

Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "Not Found" } ], "code": 404, "message": "Not Found" } }

Is there a further step I need for verification with my private calendar?

Best Dev Tutorials
  • 452
  • 2
  • 6
  • 22
  • first I'd check that you got the ID right. a 404 is not a security error. Try using the sandbox (or some PHP) to [list](https://developers.google.com/calendar/v3/reference/calendarList/list) all the calendars on the account and check their IDs. – ADyson Feb 26 '19 at 21:13
  • @ADyson When I use the listCalendarList method, I only get a permissions issue. This is because the service account doesn't have access. Where would I go from there? – Best Dev Tutorials Feb 26 '19 at 22:36
  • What is the exact error? I would imagine you need to give your service account access as the first step, if you haven't already. You can't access private data of any sort without the service account being allowed to access your calendars and also the PHP code correctly authenticating using that account. Also you appear not to have added the relevant scope for this method into your code (see the documentation page I linked you to), so maybe the error relates to that. Hard to know without any specifics. – ADyson Feb 26 '19 at 22:39
  • @ADyson I've added my the calendar onto the account. Still getting the same 404 error from my OP. is ->setScopes("https://www.googleapis.com/auth/calendar.events.readonly") not the relevant scope? – Best Dev Tutorials Feb 26 '19 at 22:49
  • As I already mentioned, for the "list" command you can check the relevant scopes at the documentation link I gave in my first comment – ADyson Feb 26 '19 at 22:52
  • @ADyson I see, I added https://www.googleapis.com/auth/calendar into my scopes. The listCalendarList() just produced nothing though as the service user isn't an actual user. I had added the account that I use for the google api dev login. But the actual service user email isn't something that can be added -- it's kind of a virtual user email that Google creates. – Best Dev Tutorials Feb 27 '19 at 00:32
  • You probably need to give the service account user permissions to your calendars. You can do it through the Google calendar UI in the sharing settings. See https://stackoverflow.com/a/27323177/5947043 for more detail. – ADyson Feb 27 '19 at 02:32

0 Answers0