Reputation: 61
I am using this library the Google php client library and following the google drive quickstart tutorial.
$client = new Google_Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
I am trying to upload my files from my PHP application to Google Drive. When I run the application to upload, it is asking for authorization. To do this its requiring that i login to my google account and consent the access to my drive account before coming back to my application.
I want to skip this step. How do i upload to google drive without requiring authorization to Google.
Upvotes: 1
Views: 1287
Reputation: 116948
What you need to understand first is that the diffrence between private and public data. Private data is owned by a user and public data is public anyone can access it.
Google drive data is private user data. In order to access it you must have the permission of the user who owns it. The most common way of getting that permission is using Oauth2 and popping up the consent screen that you are seeing in the code you have now. There is another option though.
If the account you are accessing is your own one that you as the developer have control over then you can use a service account. Service accounts are used for server to server authentication. Service accounts are preauthorized. What you will do is take the service account email address and share a directory or a file on your google drive account that you want it to have access to. Once you have granted it access it will have access it will not need to login.
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();
}
}
my sample on service accounts. serviceaccount.php
Upvotes: 2