user1639314
user1639314

Reputation: 19

Unauthorized client Google Calendar API (Google_Service_Exception)

I have setup Google calendar API project. I am using OAuth 2.0 to Access Google Calendar APIs data with consent screen.

I have followed the process mentioned here: https://developers.google.com/google-apps/calendar/quickstart/php

while I am trying to fetch all calendars for the authorized Google account. I am getting following error:

<h1>Google_Service_Exception</h1>
{
 "error": "unauthorized_client",
 "error_description": "Unauthorized"
}

Here is the code to fetch all calendars list:

$client = $this->getGoogleCalenderClient($clientSecretPath);

$accessToken = json_decode(file_get_contents($clientSecretPath), true);

$client->setAccessToken($accessToken);

if ($client->isAccessTokenExpired()) {

    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    $result = $s3Client->putObject([
        'Bucket' => $bucketName,
        'Key' => $credentialsPath,
        'Body' => json_encode($client->getAccessToken())
    ]);
}

$service = new Google_Service_Calendar($client);

$calendarList = $service->calendarList->listCalendarList();


Does anyone know what this is happening here? And how can I fix this error?
Grateful for any help.

Upvotes: 1

Views: 2825

Answers (1)

Danee
Danee

Reputation: 322

Does anyone know what this is happening here? And how can I fix this error?

If you are accessing the application using service account,

check this Github and the official document of Google for domain-wide delegation.

The following steps must be performed by an administrator of the Google Apps domain:

  1. Go to your Google Apps domain’s Admin console.
  2. Select Security from the list of controls. If you don't see Security listed, select More controls from the gray bar at the bottom of the page, then select Security from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain.
  3. Select Advanced settings from the list of options.
  4. Select Manage third party OAuth Client access in the Authentication section.
  5. In the Client name field enter the service account's Client ID.
  6. In the One or More API Scopes field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar.
  7. Click Authorize.

Upvotes: 1

Related Questions