My studying notebook

Showing posts with label Calendar. Show all posts
Showing posts with label Calendar. Show all posts

2014/01/16

ASP.NET MVC 4 using Google Calendar API via OAuth 2

1/16/2014 10:26:00 PM Posted by Unknown , , , , , 12 comments

Scenario

It's more easily to log in your application via OAuth and OpenID provider in  ASP.NET MVC 4 now. Microsoft has few build-in client for Microsoft, Twitter, Facebook, Google. The Google client is based on OpenID and not OAuth. That's mean you can not access Google Data API. There is a post talk about how to implement custom Google client with Google+ login via OAuth in ASP.NET MVC 4. (http://www.beabigrockstar.com/google-signin-for-asp-net-mvc/ Google+ Signin for ASP.NET MVC - Be A Big Rockstar )

Register a Client ID for web application in Google Developer Console

In order to access Google Data API for web application. You need to register a Client ID to get Client ID an Client Secret for setting in your application.


As above snapshot show, you need to assign redirect URIs for grap OAuth access token callback also. Here we setup Rirect URIs as

 http://localhost:57271/Account/ExternalLoginCallback 

Google Client Library for .NET

The Google APIs Client Library for .NET is generic .NET runtime client for Google Services. The library supports OAuth2.0 authentication, and is able to generate strongly typed client libraries for Discovery-based services.
Google Client library is a higher level library for using Google Data API. You can download beta version from Nuget in visual studio.

 https://www.nuget.org/packages/Google.Apis.Calendar.v3/ 
It's more difficult to handle Google Client Library for .NET with few documents and sample now. Here, just using Google.Apis.Calendar.v3.Data namespace to our strong type class for data binding in deserialize object from API response.

        private Event GoogleEventHandle(string token, string method, string requestURL, string requestBody = null)
        {
            var jsonSerializer = new JavaScriptSerializer();
            var request = WebRequest.Create(requestURL) as HttpWebRequest;
            request.KeepAlive = true;
            request.ContentType = "application/json";
            request.Method = method;
            request.Headers.Add("Authorization", "Bearer " + token);

            if(requestBody != null)
            {
                Stream ws = request.GetRequestStream();
                using (var streamWriter = new StreamWriter(ws, new UTF8Encoding(false)))
                {
                    streamWriter.Write(requestBody);
                }
            }

            var response = request.GetResponse();
            var stream = new StreamReader(response.GetResponseStream());

            var googleEvent = Newtonsoft.Json.JsonConvert.DeserializeObject(stream.ReadToEnd().Trim());

            return googleEvent;  
        }

        private Event CreateGoogleEvent(string token, string calendarId, string requestBody)
        {
            var requestURL = string.Format("https://www.googleapis.com/calendar/v3/calendars/{0}/events", calendarId);
            return GoogleEventHandle(token, "POST", requestURL, requestBody);              
        }
Above is methods how we are accessing Google Calendar v3 API via webrequest.

MVC view

Now, we are be able to access Google Calendar API via OAuth. Nest step, we will create a simple CRUD UI by AngularJS. 

The Google Event we created and save to database also.

You should choose which calendar you want to create a new event.

You can modify event anytime you want.

What you will see in your Google Calendar.

Summary

  1. It a good topic to discuss where do you store the Access token from Google OAuth. Session or database?
  2. Google Client Library for .NET is a convienient library to access Google Data API with OAuth include. It's still in developing and i hope that there have more documents and samples to show how to use it.

Reference

Github repository

As always, all source code can be found in Github in the repository
 https://github.com/cage1016/ASPNETMvc4GoogleOAuth

2009/02/23

Google Calendar Primary Calendar Change

2/23/2009 11:44:00 PM Posted by Unknown , 1 comment
Problem

when i add new calendar event in iPhone. There isn't another calendar that you choose. Default setting is primary calendar(the first calendar of your calendar lists). I often review the schedule and want to add in my wall street calendar in the Wall Street. How to Change the Google Calendar primary calendar.



Solution

When you log in your google calendar. you can see the calendar lists of Calendar Settings/Calendars. The first one (show in list is enable) is your primary calendar. You can't delete primary calendar or unsubscribe it. Maybe you had added some events in these two calendar. How to change your primary? 
  1. backup your calendar (export these calendars).
  2. delete the calendar that you want to change to primary calendar.
  3. delete the primary calendar (you can't delete this calendar truly. just clear events).
  4. this stage. you have two empty calendars.
  5. change the primary calendar name what you want. (ex: Wall Street).
  6. import backup calendars to your primary calendar

  7. create new calendar(your original primary calendar)
  8. import backup
  9. done

Conclusion

In fact, you can add new event in your different google calendars use computer easily. But when you use mobile device and this way can solve the requirement of add new event outdoor.