Search code examples
phpcodeignitergoogle-calendar-apiservice-accountsgoogle-client

Not able to generate google meet link using calendar API with service account


I Have created service account and able to done domain wide delegation but it is not working. I have done code in PHP codeigniter 3. I used google-api-php version 2 still not able to generate google-meet link but I am able to generate event in calendar. I gave calendar permission to service account also. when I try without attendees I am getting this error.

enter image description here

when I try with attendees I am getting this error.

enter image description here

Here is my sample get token code which I have done

    public function generate_token() {
        $jsonKeyFilePath = APPPATH . 'third_party/gentle-meet-399008-352ca7aa0503.json';
        $serviceAccountEmail = '[email protected]';
        $scopes = [
            'https://www.googleapis.com/auth/calendar',
            'https://www.googleapis.com/auth/admin.directory.resource.calendar',
            'https://www.googleapis.com/auth/calendar.events',
        ];

        $client = new Google_Client();
        $client->setAuthConfig($jsonKeyFilePath);
        $client->setApplicationName('Google Meet');
        $client->setAccessType('offline');
        $client->setScopes($scopes);
        $client->setSubject($serviceAccountEmail);
        if ($client->isAccessTokenExpired()) {
            $token = $client->fetchAccessTokenWithAssertion();
        } else {
            $token = $client->getAccessToken();
        }
        return $token['access_token'];
    }

    public function getRandomId() {
        $characters = 'abcdefghijklmnopqrstuvwxyz';
        $randomString = '';
        for ($i = 0; $i < 8; $i++) {
            $index = rand(0, strlen($characters) - 1);
            $randomString .= $characters[$index];
        }
        return $randomString;
    }

Here is my sample insert event code which I have done

        $accessToken = $this->generate_token();
        $data = array(
            'summary'     => "Testing Meeting",
            'description' => "test",
            'start'       => array(
                'dateTime' => "2023-09-15T20:00:00Z",
                'timeZone' => 'Asia/Kolkata',
            ),
            'end'         => array(
                'dateTime' => "2023-09-15T21:00:00Z",
                'timeZone' => 'Asia/Kolkata',
            ),
            'conferenceData' => array(
                'createRequest' => array(
                    'requestId' => uniqid(),
                    'conferenceSolutionKey' => array(
                        'type' => 'hangoutsMeet',
                    ),
                )
            ),
            'attendees' => array(
                array('email' => '[email protected]'),
            ),
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/calendar/v3/calendars/c8ac70eab58d177ca35596a8be3b8ff0a7489462683a6237ca8bf1ff6bd9dc3e@group.calendar.google.com/events?conferenceDataVersion=1");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            "Content-type: application/json",
            "Accept: application/json",
            "Authorization: Bearer ".$accessToken
        ]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        $result = curl_exec($ch);
        $errors = curl_error($ch);
        $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return $result;

I am also attaching screenshot of domain wide delegation done

enter image description here

And here is screenshot of service account

enter image description here

and here is screenshot of permission which I assign to that service account

enter image description here


Solution

  • $client->setSubject($serviceAccountEmail);
    

    set subject is the email of the user on the domain you want the service account to impersonate.