2

Eventually, I would like to upload a file to my Google Drive via App Engine php script.

However, following tutorials such as https://developers.google.com/drive/web/quickstart/quickstart-php force the individual to be signed in. I'm assuming that in this case, I would have to use Service Account credentials.

From here, I uploaded the following code along with the needed API Client Library files

<?php
require_once 'autoload.php';
//require_once 'Auth/AssertionCredentials.php';
require_once 'Client.php';
require_once 'Service/Drive.php';

//
$client = new Google_Client();

$client->setApplicationName("blah-blah-00000");
$client->setClientID("000000000000-00000000000000000000000000000000.apps.googleusercontent.com");

$service = new Google_Service_Drive($client);

// This file location should point to the private key file.
$key = file_get_contents("BlahBlah-000000000000.p12");

$cred = new Google_Auth_AssertionCredentials(
  "000000000000-00000000000000000000000000000000@developer.gserviceaccount.com",
  // Replace this with the scopes you are requesting.
  array('https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/drive.file'),
  $key
);
$client->setAssertionCredentials($cred);


//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle('HelloWorld');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = "Hello World!";

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
      'uploadType' => 'media'
    ));

//print_r($createdFile);

?>

After visiting the associated appspot.com, the page is blank. At this point, I check the Google Drive associated with the account; however no new files are found.

Am I correct in using a Service account? Provided that this is correct, my next question is am I correctly setting up the credentials? Do I have to impersonate my user account/the account that the App Engine application is setup with?

QuintoViento
  • 198
  • 3
  • 19
  • Please edit your post to include a clear question. – TPhe Apr 20 '15 at 19:13
  • Do you see any error in the log? – Mars Apr 20 '15 at 20:34
  • 24.XXX.XXX.XXX - - [20/Apr/2015:13:47:31 -0700] "GET / HTTP/1.1" 200 25 - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" "0000-0000-00000.appspot.com" ms=657 cpu_ms=250 cpm_usd=0.000089 loading_request=1 instance=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX app_engine_release=1.9.19 – QuintoViento Apr 20 '15 at 20:51
  • This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application. – QuintoViento Apr 20 '15 at 20:52
  • 24.XXX.XXX.XXX - - [20/Apr/2015:13:47:31 -0700] "GET /favicon.ico HTTP/1.1" 200 25 - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" "0000-0000-00000.appspot.com" ms=487 cpu_ms=80 cpm_usd=0.000089 instance=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX app_engine_release=1.9.19 – QuintoViento Apr 20 '15 at 20:53
  • So, no, there are no errors – QuintoViento Apr 20 '15 at 20:53
  • Please edit your question to add more information – Toby Allen Apr 20 '15 at 20:53

2 Answers2

2

Am I correct in using a Service account?

Probably not. I see a lot of people using a Service Account when what they should be using is offline access to a standard account. "At this point, I check the Google Drive associated with the account; however no new files are found". Specifically, people don't realise that a Service Account is NOT associated with their standard account, so naturally when viewing Drive for their standard account, the files from the Service Account aren't there. They're in the Service Account which has no UI.

Provided that this is correct, my next question is am I correctly setting up the credentials?

It isn't. You say " following tutorials such as ... force the individual to be signed in." There aren't any tutorials that show your use case, but that doesn't mean it can't be done. Just that nobody wrote a tutorial on how to do it.

Do I have to impersonate my user account/the account that the App Engine application is setup with?

You need to:-

  1. do a one-time authorize for your account which requests offline access. This will give you a refresh Token which you need to save.
  2. Then use the Refresh Token to request an Access Token when you want to access Drive.

The good news is step 1 doesn't require you to write any code. See How do I authorise an app (web or installed) without user intervention? (canonical ?)

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
0

After following pinoyyid's LINK and using an offline OAuth2 refresh token, I updated my code to the following

<?php
require_once 'autoload.php';
require_once 'Client.php';
require_once 'Service/Drive.php';

//
$client = new Google_Client();
// Replace this with your application name.
$client->setApplicationName("00000-00000-00000");
$client->setClientID("000000000000-00000000000000000000000000000000.apps.googleusercontent.com");
$client->setClientSecret("XXXXXXXXXXXXXXXXXXXX");
$client->refreshToken("1/KXXXXXXXXXXXXXXXXXXXXXXXXX");

// Replace this with the service you are using.
$service = new Google_Service_Drive($client);

$_SESSION['service_token'] = $client->getAccessToken();

//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle('HelloWorld');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = "Hello World!";

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
      'uploadType' => 'media'
    ));

?>
Community
  • 1
  • 1
QuintoViento
  • 198
  • 3
  • 19