Tuesday

2-Legged OAuth and Google Apps Data API

There is no doubt about the fact that implementing OAuth 2.0 protocol for a web service is not as simple as compared to using a web service which implements OAuth 2.0 protocol. The case is the same with web services provided as a part of Google Apps offering as well. It’s just a matter of few lines to get a Google Apps web service client authorized through OAuth protocol.

In this article I will demonstrate the usage of 2-Legged OAuth with Google Calendar API, which is one of Google's most commonly used API. Google Data API for Calendar implements 2-Legged OAuth protocol so you can access data transparently on behalf of other users. This can be achieved by going to Google Apps Administration Console -> Advanced Tools -> Manage OAuth domain key. Here you will have to enable two options as highlighted in following illustration:



I have created a simple application to demonstrate how to access data from Google Calendar by using 2-Legged OAuth 2.0 protocols. You can create a small application and put this code in action.

You will have to reference three dll files into your application to make the following code work, which are Google.GData.AccessControl, Google.GData.Calendar and Google.GData.Client.

using System;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Calendar;


namespace GoogleAppsOAuth
{
    public partial class Form1 : Form
    {
        const string _OWNCALENDARFEED = 
"https://www.google.com/calendar/feeds/default/owncalendars/full";

        CalendarService _Service;
        private GOAuthRequestFactory _RequestFactory;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-CA");
            LoadService();
        }

        private void LoadService()
        {
            //Initialize CalendarService
            if (_Service == null)
            {
                _Service = new CalendarService("osamamirza.com-personalapp-1.0");
                _Service.RequestFactory = GetRequestFactory();
            }
        }

        public GOAuthRequestFactory GetRequestFactory()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = 
                    System.Globalization.CultureInfo.CreateSpecificCulture
                                                ("en-US");

            if (_RequestFactory == null)
            {
                //In first two parameters you can provide any string value.
                _RequestFactory = new GOAuthRequestFactory
                             ("GOOGLE_OsamaMirza", "osamamirza.com-personalapp-1.0");
                
                //You can fetch following information from Google Apps 
                //Administration console.
                //Google Apps Administration 
                //Console -> Advanced Tools -> Manage OAuth domain key 

                _RequestFactory.ConsumerKey = "osamamirza.com";
                _RequestFactory.ConsumerSecret = "xxxxxxxxxxxxxxxxxxxx";
            }

            return _RequestFactory;
        }

        private void btnGetCalendarIDs_Click(object sender, EventArgs e)
        {
            CalendarQuery query = new CalendarQuery();
            //First paramter in OAuthUri's constructor is the 
            //called the Calendar Owner Feed. 
            //Calendar Owner Feed will get all the calendars owned by user.
            //Second parameter in OAuthUri's constructor is the user name 
            //whose data you will be accessing
            //Third parameter is the Google Apps domain name.
            query.Uri = new OAuthUri(_OWNCALENDARFEED, "username", "osamamirza.com");
            //Call CalendarService's Query function to get feed back from Google
            CalendarFeed feed = (CalendarFeed)_Service.Query(query);

            foreach (var item in feed.Entries)
            {
                tbResult.Text += GetCalendarID((CalendarEntry)item) + 
                                      " (" + item.Title.Text + ")";
            }
        }

        public string GetCalendarID(CalendarEntry calEntry)
        {
            int subStr = 63;
            //Decoding URL
            return HttpUtility.UrlDecode(calEntry.Id.AbsoluteUri.Substring(subStr));
        }
    }
}

No comments:

Post a Comment

Your comments are highly appreciated!