Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using Google's Data API in Brew: An Introduction : Page 3

Add functionality to your Brew applications with IWEB and Google's powerful Calendar API.


advertisement
Logging In
Logging in is a little bit more complicated than grabbing the public feed. The process can be a multi-step endeavor requiring tokens, SSL, and eventually even cookies. Google's documentation outlines the process for logging into a calendar account. First, you'll need to POST to this URL:

"Email=*email_address*&Passwd=*password*&service=cl&source=Devx-Sample-Code-1"

over HTTPS to:

https://www.google.com/accounts/ClientLogin.

Listing 2 shows the function to accomplish the post.



While it may be a review for experienced Brew developers, let's break down Listing 2's code a little bit. First, you'll need a few Brew objects. You need an SSL root cert to use https, an IWEB object to make the request, and a SourceUtil to format the post data for the IWEB object. Next, you can use CALLBACK_Init to set up your response callback function. In this example, the post data is hard coded to joesixpack@gmail.com and a simple password. You'll probably want to pull this information from a text control or previously cached login info. Note the "service=cl". This tells Google's servers that you want the Calendar service. Google's documentation also specifies the required content type, so you'll have to placate them and put "Content-Type:application/x-www-form-urlencoded" in the header.

Now that you have all your data, turn the post into an ISource and post the data over IWEB. Then it's just cleanup and return. As always, it's important to guard resources and memory allocations to keep your application from crashing in low resource situations.

Once you've sent out the post, you'll have to wait for the callback containing the authentication token. For reference, here's the function.

static void LoginCallback(void *pData) { char *szResp; WebRespInfo *info; GApp *pme = (GApp *)pData; if(info->nCode == 200 && pme->pResp) info = IWEBRESP_GetInfo(pme->pResp); else { DBGPRINTF("Web Resp Failure"); return; } //bla = IWEB_GetOpt(pme->pWeb, WEBOPT_HEADER, 0, &lOpt); szResp = MALLOC(info->lContentLength+1); ISOURCE_Read(info->pisMessage, szResp, info->lContentLength); //szResp = (char *)lOpt.pVal; if(bla = info->lContentLength) pme->szToken = szResp; }

The above function should look a lot like Listing 2. Allocate a pointer to hang on to your authentication token, which should be returned to you in the body of the response. Google requires that you put the token returned on the line "Auth=" into the header of your next request as "Authorization: GoogleLogin auth=". Now that you've got your login token, the rest of the information private to the account is available to you.

Go Code!
Using what you've covered in this inroductory, you've got examples for basic IWEB use, the steps necessary to grasp the XML feed for a public Calendar feed, and an example of using SSL to allow users to login to Google from within your mobile application. Perhaps you could use a public Google calendar to list upcoming events related to your application. Additionally, the process for logging into the Calendar is nearly identical to logging into other Google data services. Stay tuned to DevX for further, more advanced, examples for using Google's powerful Data API. As Google told us on their website, and on Developer Day: "Go Code!"



Chris Haseman is an independent software engineer specializing in wireless development. He can be found riding his bike between coffee shops in San Francisco. He's the author of the book Android Essentials (published by Apress). In his spare time, he's a resident DJ at xtcradio.com and a martial arts instructor.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap