Saturday, May 24, 2008

ClientLogin with PHP CURL

I find it quite hard having a comprehensive sample or documentation on using Google data services with bare PHP code. Finally - with some trials and errors - can get things done and now fully understand how to interact with GData Protocol.

One of the basic ways to interact with Google data services is to use ClientLogin, in which your web application or desktop application can authenticate into Google services using raw http and xml protocol.

So, based on Google documentation at how to use curl, I have written some PHP codes to test against each service. One that had been fully tested is Picasa.

There are several issues when I try to use ClientLogin mechanism in PHP :
  • The application doesn't know the SSL key and if we can add it dynamically, it will be limited if you are using shared host environment.
  • You have to construct your own HTTP header as authentication process part.
Thanks to curl library function that can overcome all the issues. So here are my codes that I think is quite intuitive and understandable.

But please mind that you need to understand GData protocol first before you continued on. And you will need of course, a Picasa account and some uploaded photos.

Obtaining Login Token

Here is a sample code on how to login into Picasa service (code : hl2). For other services code see here. Replace Email and Passwd entry with your own gmail address and password.

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$data = array('accountType' => 'GOOGLE',
'Email' => 'youremailaddress@gmail.com',
'Passwd' => 'yourpassword',
'source'=>'PHI-cUrl-Example',
'service'=>'lh2');

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$hasil = curl_exec($ch);

echo $hasil;
?>


Sample output format will looks like below. For the sake of simplicity, all token's long text are truncated.

SID=DQA...oUE
LSID=DQA...bbo
Auth=DQA...Sxq


You will use this Auth token for Picasa resources that is not publicly consumed. This token will last for 24 hours.

Using the service

And here I show you how to consume the service based on this reference. For this sample I use url service :

http://picasaweb.google.com/data/feed/base/user/PicasaUserID/album/PicasaAlbumID?imgmax=200.



Replace PicasaUserID and PicasaAlbumID with your real Picasa user and album id.

This will return feeds that contains information and image url of your album entries. Make sure that your album is set to unlisted album or not a public one.

The complete php source code is shown below.

<html>
<head>
<title>Picasa GData API Demo - PHP CURL</title>
<style>
.container td {
padding: 0px;
border:1px solid black;
}

.container td td {
padding: 0px;
border: 0px;
}
</style>
</head>
<body>
<?php
$ch = curl_init("http://picasaweb.google.com/data/feed/base/user/PicasaUserID/album/PicasaAlbumID?imgmax=200");

$header[] = 'Authorization: GoogleLogin auth=DQA...Sxq';

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

$hasil = curl_exec($ch);
curl_close($ch);

if(preg_match("@<\?xml version='1\.0' encoding='UTF-8'\?>@",$hasil))
{
$X = simplexml_load_string($hasil);
echo "<table cellspacing='0' cellpadding='0' class='container' width='100%'>";
echo "<tr style='color:white; background: black;'>";
echo "<td width='400px'>Summary</td><td align='center'>200pixel width</td></tr>";
foreach ($X->entry as $a){
echo("<tr>");
echo("<td>" . $a->summary . "</td>");
foreach($a->content->attributes() as $b=>$c)
{
if($b=="src"):
echo "<td align='center'><img src='$c'></td>";
endif;
}
echo "</tr>";
}
}
else
{
echo $hasil;
}
?>
</body>
</html>


Notice that we pass our token to header[] array and have it set with curl_setopt($ch, CURLOPT_HTTPHEADER, $header) function.

If all running well, you will see something like in the picture below. Here I have table of two images with a summary of each photo and a thumbnail of 200 pixel width.



Hope that you may find this article useful.


Feris Thia
Business Intelligence Consultant
http://www.phi-integration.com

11 comments:

Mister T said...

Thank's a lot, you help me a lot to begin playing with Google API's

Rajchowiak said...

it realy usefull. I was playing around with clientlogin authorisation for few houers and can not make authorisatn query to webmaster account. Thank to you it is working:)

Luca Postregna said...

What's the more simple way to create a picasa album with curl?

Unknown said...

Cool post! Thanks a lot! I was digging the google docs for several days, I wanted the client login example. Thanks a lot for sharing, this is really great.

Saqib Ali said...

Hi,

This example seems to work for my consumer gmail account. But not for my Google Apps Standard. How do I obtain Authentication Tokens for my Google Apps Standard Account?

I am trying to use:
# curl https://www.google.com/accounts/ClientLogin -d Email=testuser2@quantumcrypto.de -d Passwd=password -d accountType=GOOGLE -d source=Google-cURL-Example -d service=lh2

Saqib Ali said...

My fault. I should have used accountType=HOSTED_OR_GOOGLE.

Thanks.

Unknown said...

I have a little bit and I could use google analytics data. Thank you very much.

Andres said...

hi
this is an example that works
I only need to upload an image
how I can upload an image?
all methods I have always an error
some example?
Thank

Adam Stacey said...

Fantastic article. Just what I needed. There is very little out there discussing this. Many thanks.

Unknown said...

OH! Thanks a lot, guy! You really helped me!

Unknown said...

Usefull!

...but could you help me to point my calendar with a simple user that only have read accessount. The authentication should be OK but what code point the calendar?


$ch = curl_init("https://www.google.com/calendar/embed?src=q7nr5gff
$header[] = "'Authorization: GoogleLogin auth=".$auth."'";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
...
????


Thank for Help, Horst
horst.hart68@gmail.com