Reputation: 11
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 .
JPEG data
Upvotes: 1
Views: 3083
Reputation: 867
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.
Upvotes: 0
Reputation: 17613
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.
Upvotes: 0