I have a PHP code that uses the Google PHP Client API to use the Drive service.
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
const CLIENT_ID = '[MYCLIENTID]';
const SERVICE_ACCOUNT_NAME = '[MYSERVICEACCOUNID]';
const KEY_FILE = './[MYPRIVATEKEY].p12';
$client = new Google_Client();
$client->setApplicationName("Google Drive Sample");
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive'),
$key)
);
$client->setClientId(CLIENT_ID);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$token = $client->getAccessToken();
echo "<pre>";
$service = new Google_Service_Drive($client);
$service->apps->listApps();
echo "</pre>";
?>
I had CLIENT_ID, SERVICE_ACCOUNT_NAME and KEY_FILE set. When I run the code, I get the following error messages:
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/drive/v2/apps: (403) Insufficient Permission' in C:\xampp\htdocs\oauth\Google\Http\REST.php:79
Stack trace:
thrown in C:\xampp\htdocs\oauth\Google\Http\REST.php on line 79
What could be wrong with the code?
source
share