Upload File from App Engine to Google Drive - php

Eventually, I would like to upload a file to my Google Drive via App Engine php script.
However, following tutorials such as https://developers.google.com/drive/web/quickstart/quickstart-php
force the individual to be signed in. I'm assuming that in this case, I would have to use Service Account credentials.
From here, I uploaded the following code along with the needed API Client Library files
<?php
require_once 'autoload.php';
//require_once 'Auth/AssertionCredentials.php';
require_once 'Client.php';
require_once 'Service/Drive.php';
//
$client = new Google_Client();
$client->setApplicationName("blah-blah-00000");
$client->setClientID("000000000000-00000000000000000000000000000000.apps.googleusercontent.com");
$service = new Google_Service_Drive($client);
// This file location should point to the private key file.
$key = file_get_contents("BlahBlah-000000000000.p12");
$cred = new Google_Auth_AssertionCredentials(
"000000000000-00000000000000000000000000000000#developer.gserviceaccount.com",
// Replace this with the scopes you are requesting.
array('https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file'),
$key
);
$client->setAssertionCredentials($cred);
//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle('HelloWorld');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = "Hello World!";
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'text/plain',
'uploadType' => 'media'
));
//print_r($createdFile);
?>
After visiting the associated appspot.com, the page is blank. At this point, I check the Google Drive associated with the account; however no new files are found.
Am I correct in using a Service account? Provided that this is correct, my next question is am I correctly setting up the credentials? Do I have to impersonate my user account/the account that the App Engine application is setup with?

Am I correct in using a Service account?
Probably not. I see a lot of people using a Service Account when what they should be using is offline access to a standard account.
"At this point, I check the Google Drive associated with the account; however no new files are found". Specifically, people don't realise that a Service Account is NOT associated with their standard account, so naturally when viewing Drive for their standard account, the files from the Service Account aren't there. They're in the Service Account which has no UI.
Provided that this is correct, my next question is am I correctly setting up the credentials?
It isn't. You say " following tutorials such as ... force the individual to be signed in." There aren't any tutorials that show your use case, but that doesn't mean it can't be done. Just that nobody wrote a tutorial on how to do it.
Do I have to impersonate my user account/the account that the App Engine application is setup with?
You need to:-
do a one-time authorize for your account which requests offline access. This will give you a refresh Token which you need to save.
Then use the Refresh Token to request an Access Token when you want to access Drive.
The good news is step 1 doesn't require you to write any code. See How do I authorise an app (web or installed) without user intervention? (canonical ?)

After following pinoyyid's LINK and using an offline OAuth2 refresh token, I updated my code to the following
<?php
require_once 'autoload.php';
require_once 'Client.php';
require_once 'Service/Drive.php';
//
$client = new Google_Client();
// Replace this with your application name.
$client->setApplicationName("00000-00000-00000");
$client->setClientID("000000000000-00000000000000000000000000000000.apps.googleusercontent.com");
$client->setClientSecret("XXXXXXXXXXXXXXXXXXXX");
$client->refreshToken("1/KXXXXXXXXXXXXXXXXXXXXXXXXX");
// Replace this with the service you are using.
$service = new Google_Service_Drive($client);
$_SESSION['service_token'] = $client->getAccessToken();
//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setTitle('HelloWorld');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = "Hello World!";
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'text/plain',
'uploadType' => 'media'
));
?>

Related

Service Account - Uploaded PPTX files not showing in my Google Drive

This should be strange moment when uploading files with Service account to my google drive is not showing inside the google drive.
I read somewhere the service account files will not show in my google drive. then what is the point of service account ? and how can i upload files in my google drive account via service account ?
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require __DIR__ . '/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=credentials.json');
$client = new Google\Client();
if (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
// use the application default credentials
$client->useApplicationDefaultCredentials();
} else {
echo missingServiceAccountDetailsWarning();
return;
}
$client->setApplicationName("Client_Library_Examples");
//$client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setName('test-1.pptx');
$file->setDescription('A test document');
$file->setMimeType('application/vnd.openxmlformats-officedocument.presentationml.presentation');
$data = file_get_contents('test-1.pptx');
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'uploadType' => 'multipart'
));
echo '<pre>';
var_dump($createdFile);
// Print the names and IDs for up to 10 files.
$optParams = array(
'pageSize' => 10,
'fields' => 'nextPageToken, files(id, name)'
);
$results = $service->files->listFiles($optParams);
var_dump($results);
Service accounts are dummy users. They have their own google drive account. When you upload a file by default it will go to the service accounts Google drives account.
Now if you want to upload a file to your personal drive account. what you can do is share a folder on your google drive account with the service account basically take the service accounts email address the one with a # in it and share the directory with it though the web app like you would any other user.
Now the service account will instantly have access to it this is called preauthorized. By preauthorizing the service account there will be no need for user interaction to access that folder.
You appear to be trying to create a new file all you need to do is set the parent folder of the file in the metadata before you upload it.
$file->setParents('FolderIdFromYourDrive');
Do a file.list first on the service accounts drive account if you want to find out what folder is is or you can just look at the top in the url bar.
Service accounts are nice if your application has some static data that it needs to access. This can be done by placing the files on drive and then granting the service account access to those files. Service accounts are great because they are preauthorized and you dont need the consent screen to access private user data.
You can also upload the files to the service accounts drive account and share them with your personal google account and you will have access that way.
Answer
A service account is a special kind of account used by an application or a virtual machine (VM) instance, not a person.
Option 1: Use Domain-Wide Delegation
Service accounts can access user's data without any manual authorization on their part using Domain-Wide Delegation of Authority.
With that being said, you can upload files into your Google Drive using a service account. Follow the steps in the documentation to create the service account and credentials. It has three main points:
Create the service account in the Google Cloud Console
Enable the domain-wide delegation in the Google Admin console
Instantiate a Drive service object. Check the second last line:
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setApplicationName("Client_Library_Examples");
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setSubject('email#domain.com');
$service = new Google_Service_Drive($client);
Option 2: Share the file with your main account
...
$service = new Google_Service_Drive(client)
$role = 'writer';
$userEmail = 'user#gmail.com';
$fileId = 'The ID of the file to be shared';
$userPermission = new Google_Service_Drive_Permission(array(
'type' => 'user',
'role' => $role,
'emailAddress' => $userEmail
));
$request = $service->permissions->create(
$fileId, $userPermission, array('fields' => 'id')
);
Reference
Service accounts
Domain-Wide Delegation of Authority
Google Cloud Console

Server-to-Server App using Google Drive not responses to APIs calls

I'm working on a project where I have to make a server to server app with PHP.
The concept is : the company uploads bills to Google drive and the users use the website to retrieve their bills. First of all I didn't use a composer to the server because I have no permission to install. So I downloaded the libs and uploaded to server with ftp. Then I created a G Suite Google account and followed the instructions from their guide https://developers.google.com/api-client-library/php/auth/service-accounts.
The weird thing is if var_dump(service) after $results = $service->files , I see that the verification for the client has be done. When I try to use the api with $results = $service->files->listFiles($optParams); the page doesn't respond. Im really confused with this.
<?php
session_start();
require_once "vendor/autoload.php";
require 'init.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=service.json');
$client = getClient();
$service = new Google_Service_Drive($client);
function getClient() {
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
return $client;
}
$optParams = array(
'pageSize' => 5,
'fields' => 'files(webViewLink, name , parents, id)'
);
$results = $service->files->listFiles($optParams);
Solved it. The problem was PHP version. I switched to 7.2 and seems to work perfect!

How to browse files from projects google drive

i have created project in google console with service type credentials. I have downloaded p12 key file, and then wrote sample code for inserting file to test it:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";
$scopes = array('https://www.googleapis.com/auth/drive');
$accountEmail = 'SECRET';
$accountP12FilePath = './key.p12';
$key = file_get_contents($accountP12FilePath);
$auth = new Google_AssertionCredentials($accountEmail, $scopes, $key);
$client = new Google_Client();
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);
$service = new Google_DriveService($client);
$file = new Google_DriveFile();
$file->setTitle('Test Title');
$file->setDescription('test description');
$parent = new Google_ParentReference();
$file->setParents(array($parent));
$createdFile = $service->files->insert($file, array(
'data' => 'test data',
'mimeType' => 'text/plain'
));
var_dump($createdFile);
?>
It dumps Google_DriveFile object with many urls. Some of them, like 'downloadUrl', returns 401 Unauthorized error. When I tried 'alternateLink' it showed google drive page with information that i need access, and buttons to request access and to switch account.
It also said that Im currently logged on my account, that was beeing used to create project, and that is visible on Permissions page of the project on the google console page.
How can I access drive that belongs to the project I used in the script ?
Finally found solution, it was missing permissions on file. Its worth to note that passing permission object to the $file object (before insertion) didn't work. I had to insert permission by passing already created file ID and permission object to the $service->permissions->insert method, just like that:
$permission = new Google_Permission();
$permission->setValue('PERSONAL.ACCOUNT#gmail.com');
$permission->setType('user');
$permission->setRole('writer');
$service->permissions->insert($createdFile->getId(), $permission);
Still I wasn't able to actually edit this file. When I opened file with URL
$createdFile->getAlternateLink();
Then I had to reopen that file with google docs. That copied file to my personal account and then I could only edit only that copy of a file, not a base file itself.
I dont know if there is a way to make that files truly editable, but I moved to the dropbox, as google drive API and its documentation SUX.

Is the installation tutorial outdated? Google API novice here

https://developers.google.com/drive/web/quickstart/quickstart-php#step_3_set_up_the_sample
Trying to run the quickstart.php file i find that it does absolutelly nothing.
Checking the code and comparing it to my files i see the folder and files names are different. I download the library from github and i also noticed the src folder was updated 2 days ago.
I tried just to chage them to make the quickstart run but yet it does nothing.
Im trying to authorize google drive to upload files from a page.
I already got my secret key and id, hello world and all those steps, i just cant make the quickstart to run.
I dont know how to use subversion, i just want to upload files, can somebody help me? what am i missing?
<?php
require_once 'google-api-php-client/src/Google_Client.php'; //incorrect files
require_once 'google-api-php-client/src/contrib/Google_DriveService.php'; /incorrect files
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId('already changed');
$client->setClientSecret('already changed');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();
//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = file_get_contents('document.txt');
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'text/plain',
));
print_r($createdFile);
?>
So i found this:
Submitting file to Google drive
The code is indeed outdated, i did these changes:
require_once 'google-api-php-client/src/Google/Client.php';
require_once 'google-api-php-client/src/contrib/Google/Service.php';
instead of Google_DriveService just use Google_Service.
implement the code posted by user3407337 on the previous link.
That should do the trick.

Google Drive upload PHP programmatic sign in

I am developing a web API for a mobile app which needs to store files. Currently, the mobile clients send a request to the API with the file's data, and the file is stored directly on the server. However, I need to save space on the server and this is just a temporary solution.
What I would like to do is have the mobile client upload the file to the API, which then uploads that file to Google Drive. I have taken a look at the Google Drive API and I got it to upload a file from my home computer successfully, but in order to do so, I needed to sign in to my google account to authorize the upload. Here is the code:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId(' my client id ');
$client->setClientSecret(' my client secret ');
$client->setRedirectUri('https://www.hwassassin.net16.net/oauth2callback');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();
//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = file_get_contents('document.txt');
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'text/plain',
));
print_r($createdFile);
?>
Since I need to upload the file from the backend, I would like to know if I can somehow programmatically log in to my Google Drive account from the server, without requiring the Client to take any action.
If this is not even possible, I would appreciate any alternative cloud storage API's that don't require the client to sign in to an account.
Any help would be greatly appreciated!
Solution is using application owned accounts using service accounts or regular accounts. Please follow the directions at https://developers.google.com/drive/web/service-accounts.

Categories