After you have done this you will get some more boxes in page, and one of them will show you your client secret (don't share it with anyone) and client id. You have done first step of 5 step process in making your own application. After this go to https://developers.google.com/drive/quickstart , and under the heading Install the google client library choose php. There is one command that's shown there.
This is what I have done till now, and from now onwards I will blog simultaneously.
It's 1412 hrs and I just copied that one line command we got in previous step and pasted it in terminal. I got an error, svn is not installed and error tells I can install it using
$sudo apt-get install subversion
It's 1414 hrs subversion is installed.I re-executed the command that I got in previous step i.e
$ svn checkout http://google-api-php-client.googlecode.com/svn/trunk/ google-api-php-client
and what i get is shown in following picture
Now if i preform $ls and I can see that I have got an extra folder named google-api-php-client in the current folder.
It's 1421 hrs and I am at Step 3 of 5 steps needed to make a drive console application. Third step is to setup the sample.
To setup the sample I need two things, a source code and a text file. I created a text file with $nano mytextfile.txt and added some sample content. I also created a drive.php file and pasted this code in the file.
<?php require_once 'google-api-php-client/src/Google_Client.php'; require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
// this is google client library and you got to include it in order to use it. $client = new Google_Client(); // Get your credentials from the APIs Console $client->setClientId('YOUR_CLIENT_ID');
// add your client id and secret(you got it when you created your account) $client->setClientSecret('YOUR_CLIENT_SECRET'); $client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
//This is standard Uri for installed applications,may change in web applications. $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); ?>
Its 1425 hrs and I got a call from vigasdeep, and I told him about this stuff.
It's 1440 hrs, I have a power cut in my town, I just had a glass of water and now I am ready to execute my php file.
Oops! My php is not yet installed, so firstly i will install php using
$sudo apt-get install php5-cli;
It's 1443hrs and Php5 is installed. Now I will try to run my file. I got an error
PHP Fatal error: Uncaught exception 'Exception' with message 'Google PHP API Client requires the CURL PHP extension' in /home/inderpreet/google-api-php-client/src/Google_Client.php:21
So it says i need PHP CURL extension, let's install it then
sudo apt-get install php5-CURL
also I realised that I need to write name of my text file, in place of document.txt
It's 1454hrs and I have installed php5-curl extension. Let's try running php file again.
Yepeee! it's 1455 and php file is running. For those who have never run a php file like this, here's a screenshot of what you will get.
You will get a url, click on url to go to that url, or copy paste it in your browser. You will get this:-
My application running in browser |
We have a title, called my document, link to our file, it's type description and lots of stuff. You can go to your drive now, and you will see you have a new document called My document with the same text that you posted in your .txt file.
It's 1500 hrs and I have created my first drive application. We have done four steps and fifth step is to learn more and that I leave it to you. This was some wonderful time. You can do much more than this using drive api, go to their documentation and can learn more about it.Just a few revisions and this post is published.
WTF? You just copied the code from Google and changed the comments! Pretty shameful.
ReplyDeleteYes sir, it's the same code you see at google examples and pretty much everything else is new. What's shameful in it?
DeleteIf you actually read the post It's actually my walk-through of how I learned to make my first PHP Google drive application and I have very well written that I am using the same code of as google. May be you must have read the post before commenting.
Better explication than tutorial
ReplyDeleteThanks :)
DeleteHi, do you know if an SSL server is required for this API to work on a production server??
ReplyDeleteI wonder cause I tried to do make this example work on my server and I noticed that google by default prepends htts://to_the_domain.com
thx
Hi, thanks for this, very well explained. Unfortunately this script will work only the first time we run it, then we have to play around with refreshing token... have u got any idea how to achieve that?
ReplyDeleteThanks again