Making your first PHP Google Drive Application


 It's afternoon here, clouds and sun are playing hide and seek. I have exams day after tomorrow, have broken various assignment deadlines and now trying to make a console application for google drive using recently released google drive api and sdk. First of all I went to developers.google.com and clicked on Api Console icon, Clicked on create project, and then from the list that came enabled google drive api and sdk. After that went and click API access to create credentials. Drive API uses OAuth2. Create credentials is a step by step process, at first screen add your logo, name of your application. At the second screen when it asks whether you want web application, installed application,etc. Click on Installed application and you will have an option to choose operating system, options are for ios,android and other. Click on other.



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

I executed this command,and now subversion is getting installed.

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

 Click on allow acces, and then you will get an auth code copy paste the auth code to terminal, and press enter. Wait for few seconds (if not less) and you shall get a lot of stuff on your terminal. It's actually json structure and indicates that your file was uploaded to the google drive and google drive has sent back this information. This information is quite useful, let's see what we have here.


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.


6 comments:

  1. WTF? You just copied the code from Google and changed the comments! Pretty shameful.

    ReplyDelete
    Replies
    1. Yes sir, it's the same code you see at google examples and pretty much everything else is new. What's shameful in it?
      If 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.

      Delete
  2. Better explication than tutorial

    ReplyDelete
  3. Hi, do you know if an SSL server is required for this API to work on a production server??
    I 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

    ReplyDelete
  4. 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?

    Thanks again

    ReplyDelete