Skip to content

Instantly share code, notes, and snippets.

@ianbarber
Created March 3, 2015 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianbarber/8d3489c41e831271c999 to your computer and use it in GitHub Desktop.
Save ianbarber/8d3489c41e831271c999 to your computer and use it in GitHub Desktop.
Query Drive with a service account
<?php
require_once "../vendor/autoload.php";
$client = new Google_Client();
$client->setApplicationName("Service Acccount Test");
$client_id = '985145966381-is5nq94smihmoa2k0567jab6u6vj278c.apps.googleusercontent.com';
$service_account_name = '985145966381-is5nq94smihmoa2k0567jab6u6vj278c@developer.gserviceaccount.com';
$key_file_location = 'foostarter-a27f84b2dec4.p12';
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array(Google_Service_Drive::DRIVE_READONLY),
$key
);
$cred->sub = "ian@example.com"; // For the @example.com Apps delegation.
// Remember to cache the access token locally if making multiple calls
// and don't just use this function for each request!
$client->getAuth()->refreshTokenWithAssertion($cred);
$service = new Google_Service_Drive($client);
$result = $service->files->listFiles();
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment