0

I have read this SO:How upload to MY Google Drive account, not the users

and I found its possible, though I therefore decide not to use a service account and use my own google account's Drive space instead.

However, I found difficulties in understanding the exact steps to achieve this. I read the OAuth2.0, Drive API, and cros-upload sample from Google.

But I do not really know how to proceed to make an upload to MY drive instead of when the user logged in Google+, then it become uploading to their own drive.

I wonder if anyone know exact knowledge I should be obtaining and steps and implementation required to achieve this uploading file from any registered users to MY google Drive with Drive API. I am using PHP as bakend and AngularJS as front-end. So, if that made possible easier with either method or existing lib (which I can't find except Drive's doc on this), will be ideal, but a steps to build one will be wonderful as well.

More detail, in the post I showed on top, the 2nd answer's phrase of "do a one-off auth to get a refresh token, which you will store. You can then use this any time to generate the access token needed to invoke the actual Drive API. You can generate the refresh token using the Oauth Playground, so no need to write any code." get me really not clear exactly the steps that involved and api that I actually should be using to achieve it.

Thank You

Community
  • 1
  • 1
ey dee ey em
  • 7,991
  • 14
  • 65
  • 121
  • 1
    If it's not possible to do this completely from the client you could have the user upload to your server which in turn uploads to Google Drive. There seems to be a PHP API https://developers.google.com/drive/web/examples/php – Popnoodles Oct 28 '14 at 19:24
  • @Popnoodles Do you think a shared hosting server will be able to do it? If not, would you mind to share the reason? Thanks! – ey dee ey em Oct 28 '14 at 19:26
  • I think @Popnoodles is correct, and yes this should be possible via a shared hosting server. – Arthur Thompson Oct 29 '14 at 16:50
  • The comment from Popnoodles is incorrect. What you want to do is possible. See http://stackoverflow.com/questions/19766912/how-do-i-authorise-a-background-web-app-without-user-intervention-canonical – pinoyyid Nov 24 '14 at 10:09
  • @pinoyyid Thanks for sharing your answer! I will definitely have a vist on it! Will let you know if worked :) – ey dee ey em Nov 24 '14 at 16:11

1 Answers1

1

To send files to Google Drive, you need an access token to use the Drive user's storage, this token expires in a few moments from creation.

To create an access token, you need a refresh token, which does not expire. This is the key to doing this.

Here is a simple file upload example using the PHP api:

https://github.com/numsu/google-drive-sdk-api-php-insert-file-parent-example

Set it up by following the readme.

To edit the code to send the files always to a single Drive from anywhere, you need to get the destination Drive's refresh token, in this case yours. You will get it by logging in to the service and granting privileges on the Google Consent screen. It will be saved in $_SESSION["credentials"]. Do a var_dump() on that and save the refresh token somewhere, in a file or a database where you can access it.

You can now remove anything that points to the user logging in with oAuth, this doesn't need that.

Next you need to get yourself an access token with the refresh token you have. This you can do with the google API function $client->refreshToken($refreshToken);

Next you just need to generate a new drive service class using the $client variable: $service = new Google_Service_Drive($client);

After that, to upload the file:

$file = new Google_Service_Drive_DriveFile();
$file->setTitle($title);
$file->setMimeType($mimeType);

$data = file_get_contents($filename);
$createdFile = $service->files->insert($file, array(
        'data' => $data,
        'mimeType' => $mimeType,
        ));

Good luck!

numsu
  • 472
  • 3
  • 10