Sunday, June 12, 2011

Get URL Parameter from Published Google Apps Script

Google Apps Script getting popular and more popular nowadays. But I found quite hard to see any samples created from independent blogger on the usages. I'd like to take the chance in some areas.

With exposed public google apps script as a service, we need to have a doGet(e) method implemented. To capture url parameter we use this construct :

e.parameter.ParameterName;

for example, explore this script modified from google documentation :

function doGet(e) {
  var app = UiApp.createApplication();
  
  // hello world label
  var helloworldLabel = app.createLabel("I love Apps Script!").setStyleAttribute("fontSize","16px");
  var verticalpanel = app.createVerticalPanel();
  var mybutton = app.createButton(e.parameter.test);
  verticalpanel.add(helloworldLabel);
  verticalpanel.add(mybutton);
  app.add(verticalpanel);
  // add the label to the app container
  //app.add(helloworldLabel);
  
  return app;
}

Notice at line 7, we used URL parameter named "test" as a caption for button element. Exposed this script as service, and try to add url parameter "test" with some value and see the result.

Below is a screenshot of the service execution :

Button Caption comes from URL parameter "test"

Tuesday, March 16, 2010

List of Google Data API Services


Google APIService name
Google Analytics Data APIsanalytics
Google Apps Provisioning APIsapps
Google Base Data APIgbase
Google Sites Data APIjotspot
Blogger Data APIblogger
Book Search Data APIprint
Calendar Data APIcl
Google Code Search Data APIcodesearch
Contacts Data APIcp
Documents List Data APIwritely
Finance Data APIfinance
Gmail Atom feedmail
Health Data APIhealth
weaver (H9 sandbox)
Maps Data APIslocal
Picasa Web Albums Data APIlh2
Sidewiki Data APIannotateweb
Spreadsheets Data APIwise
Webmaster Tools APIsitemaps
YouTube Data APIyoutube


Source : http://code.google.com/apis/documents/faq_gdata.html

Friday, August 1, 2008

Enable CURL extension in XAMPP

If you want to use curl extensively in your XAMPP distribution, here are the steps :
  1. Locate your php.ini configuration

    Open http://localhost/xampp/phpinfo.php in your browser and locate for "Loaded Configuration File" entry. This php.ini file that we should edit.


  2. Edit your php.ini to enable php_curl extension

    Remove semi colon in front of php_curl.dll extension entry.










  3. Restart your XAMPP
  4. Check for your curl extension

    Open http://localhost/xampp/phpinfo.php in your browser and locate for your curl extension entry.



  5. Done

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