I'm learning the Drive API and have read all these docs so far. I'm just trying to make an authenticated call to list the files in drive.
This is for an internal application that doesn't need user auth, I just need access to our company drive. So a "Service Account" sounds like the right thing.
Here's what I've done so far
- Created an app in Google Developer Console
- Enabled both Drive API and Drive SDK
- Downloaded and stored my p12 key
- Added my client ID and drive permissions in the Google App Admin under advanced settings as dictated here https://developers.google.com/accounts/docs/OAuth2ServiceAccount
Here's my code for listing files
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
require_once 'google-api-php-client/autoload.php';
$client_id = '...'; //Client ID
$service_account_name = '...'; //Email Address
$key_file_location = '...'; //key.p12
$client = new Google_Client();
$client->setApplicationName("...");
$service = new Google_Service_Drive($client);
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file'),
$key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$results = $service->files->listFiles();
debug($results);
?>
My results are a single file called "How to get started with Drive". So obviously it isn't authenticating with my account to show my drive files.
Any idea how to get this service account associated with my drive account so I can get a list of files?