1

I want to use Google Drive to store my application files without user authentication. I have already seen Use Google Drive API with Laravel but the answers do not match with my needs.

I have also tried an ajax post request when form submitted.

I want to know how do I get this request that I have found in Google Drive documentation: https://developers.google.com/drive/v3/web/manage-uploads .

  1. POST /upload/drive/v3/files?uploadType=media HTTP/1.1
  2. Host: www.googleapis.com
  3. Content-Type: image/jpeg
  4. Content-Length: number_of_bytes_in_file
  5. Authorization: Bearer your_auth_token

JPEG data

Community
  • 1
  • 1
naamane ilias
  • 11
  • 2
  • 2
  • i dont think they will allow it without auth otherwise anyone could upload to anyones account when they want, the auth is there to allow access?? Hence the `Bearer your_auth_token` so you will need to do the auth during the ajax call etc before uploading. – Simon Davies Aug 06 '16 at 15:56
  • yes I agree , I am still newbie of ajax request , can you please help me ? – naamane ilias Aug 06 '16 at 16:30
  • all i can recommend is searching for laravel / php and goole drive api, as you will need to set up secret keys etc and use somethign like guzzle to connect... – Simon Davies Aug 07 '16 at 00:35
  • ok thanks , I think it is impossible to get all uploaded data centralized in one google drive account ? is that right ? everyone can only upload to his own drive via the api . – naamane ilias Aug 07 '16 at 23:30

2 Answers2

0

As have been mentioned in the comment and from Daimto's answer, Google Drive (and other SaaS tools) requires authentication, for security purposes.

Try the PHP Quickstart in Drive API v3. The basic structure is already there.

For an actual example of uploading a file using the API, this SO thread might help:

require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';

$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('<YOUR_CLIENT_ID>');
$client->setClientSecret('<YOUR_CLIENT_SECRET>');
$client->setRedirectUri('<YOUR_REGISTERED_REDIRECT_URI>');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

session_start();

if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
} else
$client->setAccessToken($_SESSION['access_token']);

$service = new Google_Service_Drive($client);

//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');

$data = file_get_contents('a.jpg');

$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart'
));

print_r($createdFile);

} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit();
}

There's also a google/google-api-php-client in Github. The guide might offer you insight as well.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
0

Internally, all files and folders in Google Drive must have an authenticated user as an owner. Anyway, it should be possible to wrap around your solution with a "proxy" which uploads the file using your own account or a Google Service Account.

You will present a web page to your end user where anonymous access is allowed, then after getting the data uploaded, you will call the API in the background with your own account or a Google Service Account.

some1
  • 857
  • 5
  • 11