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.
Related
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.
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'
));
?>
When I run the API from php the only item returned is the "How to get started with Drive" file which isn't in the folder, but when I run the retrieveListOfAllFiles() from the API webpage I see all the files in the drive. Here is the code.
set_include_path(ABSPATH . "path-to/api/google-api-php-client/src/");
require_once "Google/Client.php";
require_once "Google/Service/Drive.php";
require_once "Google/Auth/OAuth2.php";
require_once "Google/Auth/AssertionCredentials.php";
define('CLIENT_ID', 'xxxxx');
define('SERVICE_ACCOUNT_NAME', 'xxxxx');
$key = file_get_contents(ABSPATH . "path-to-.p12");
$scopes = array('https://www.googleapis.com/auth/drive.readonly');
$client = new Google_Client();
$client->setApplicationName("xxxx");
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$auth = new Google_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
$scopes,
$key
);
$client->setAssertionCredentials($auth);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($auth);
}
$_SESSION['service_token'] = $client->getAccessToken();
$service = new Google_Service_Drive($client);
$files = retrieveAllFiles($service);
Files only returns the "how to get started with drive" file - where as the API test from this page: https://developers.google.com/drive/v2/reference/files/list returns all the files in the drive.
The Ids are correct, the .p12 file is new and loading, I have no idea what is going wrong.
I have also noticed that if I var_dump() the output of this
dump($service->files->listFiles(array()));
I also only get the single "how to get started with drive" file. So it seems like even though I'm not getting any OAuth2 errors, and getting a key token, etc, the drive I want to list the files from is not being accessed.
Problem resolved. A folder from the Drive must be shared with the API user - the email ending in #developer.gserviceaccount.com - which I didn't see in the docs anywhere... maybe I'm blind. But those API docs are pretty out of date, it seems.
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.
I thought this one was going to be a breeze. Hence I'm stuck at the very beginning :-(
I created a Google Calendar where users can add events using a 'shared' dedicated login.
Then I need to write a PHP script on my server to read the events in a given time frame.
I thought the API key described here would be enough. But it's not.
curl https://www.googleapis.com/calendar/v3/users/me/calendarList?key=mykey
says Login required.
So I read about OAuth2.0 and how I need user to authenticate. The problem is my script is not interactive (although hardcoding the login in the script is not a problem to me: The info is not life-critical). So I read about Service Accounts but it looks like it's for non-user info.
Question: How do I code my script to force a given login without involving a human?
Note: This SO question seemed promising but the answer is for API version 2.0, which seems obsoleted.
The first thing you need to do is obtain an access token. This will require a human.
Assuming you're using the Google APIs PHP Client, it can be done with this script (run from the command line).
NOTE: The snippet below works with a Client ID for Installed Applications. Make sure you create a client ID of this type in the Google API Access console.
require_once '../../src/apiClient.php';
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
$client = new apiClient();
// Visit https://code.google.com/apis/console to create your client id and cient secret
$client->setClientId('INSERT_CLIENT_ID');
$client->setClientSecret('INSERT_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array(
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.readonly',
));
$authUrl = $client->createAuthUrl();
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
$_GET['code'] = $authCode;
$token = $client->authenticate();
var_dump($token);
This will give you a json encoded string containing your accessToken and refreshToken. The accessToken expires after 1 hour, but don't worry. The refreshToken will not expire (unless you uninstall the application), and can be used to obtain a new refreshToken. The client library takes care of this part.
Next, save the full json string token (not only the access_token property in a safe place and make sure it can't be read by others. Your app can then call $client->setAccessToken($token) where $token was looked up from the safe location (Again, $token is the full json-encoded string, not limited to its access_token property).
Now, you can make authenticated request to the Calendar APIs!
require_once '../../src/apiClient.php';
require_once '../../src/contrib/apiCalendarService.php';
session_start();
$client = new apiClient();
$client->setApplicationName("Google Calendar PHP Sample Application");
$cal = new apiCalendarService($client);
$client->setAccessToken($tokenFromSafePlace);
$calList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
Using the latest version of the client library (for the v3 api?) to get the token you need:
require_once 'Google/Client.php';
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
$client = new Google_Client();
// Visit https://console.developers.google.com/ to create your application and client id for a native app.
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array(
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.readonly',
));
$authUrl = $client->createAuthUrl();
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
$_GET['code'] = $authCode;
$token = $client->authenticate($authCode);
var_dump($token);
To get the calendar list you then need the following code:
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';
$client = new Google_Client();
$client->setApplicationName("My Calendar example");
// token you got from the previous code
$client->setAccessToken($token);
$calendarService = new Google_Service_Calendar($client);
$calendarList = $calendarService->calendarList;
$calList = $calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";