How to access my google drive from php script without manual authorisation
Get the solution ↓↓↓Solution:
After a long research and reading google documentation and examples I found a way that works for me.
- You need a service account. This account will access the google drive data.
- I work with google domain, therefore I needed to grant domain wide authority to this service account.
- Here you can find how to create a service account and grant it domain wide authority however this link does not has PHP examples
- Here you can find the same instructions with PHP examples
- Please pay attention that you need JSON type key file when you create a service account.
- I used Google Application Default Credentials.
Finally this is working code snippet:
<?php
require_once '/path/to/google-api-php-client/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/drive']);
$client->setSubject('email_of_account@you_want_to_work.for');
$service = new Google_Service_Drive($client);
//Create a new folder
$fileMetadata = new Google_Service_Drive_DriveFile(
array('name' => 'Invoices',
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array('fields' => 'id'));
echo $file->id;
?>
Answer
Solution:
I recommend you look into using a service account. A service account is like a dummy user which is pre authenticated. This means that you can share a folder on your google drive account with the service account and the service account will be able to upload to it. I have an article on how Service accounts work. Google Developers console Service account
There are a few things you need to remember when working with service accounts though. Mainly permissions when the service account uploads a file by default it owns the file so you will need to grant your personal account permissions to the file after it is uploaded. Its just an extra step.
The PHP client library has a sample for using service account authentication but it doesn't have drive you will have to alter the books part to be google drive. Service-account.php
require_once __DIR__ . '/vendor/autoload.php';
// Use the developers console and download your service account
// credentials in JSON format. Place the file in this directory or
// change the key file location if necessary.
putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/service-account.json');
/**
* Gets the Google client refreshing auth if needed.
* Documentation: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
* Initializes a client object.
* @return A google client object.
*/
function getGoogleClient() {
return getServiceAccountClient();
}
/**
* Builds the Google client object.
* Documentation: https://developers.google.com/api-client-library/php/auth/service-accounts
* Scopes will need to be changed depending upon the API's being accessed.
* array(Google_Service_Analytics::ANALYTICS_READONLY, Google_Service_Analytics::ANALYTICS)
* List of Google Scopes: https://developers.google.com/identity/protocols/googlescopes
* @return A google client object.
*/
function getServiceAccountClient() {
try {
// Create and configure a new client object.
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope([YOUR SCOPES HERE]);
return $client;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
This may also help my google drive samples
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mark bundle as not supporting multiuse
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.