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.DeserializeObjectAbove is methods how we are accessing Google Calendar v3 API via webrequest.(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); }
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
- It a good topic to discuss where do you store the Access token from Google OAuth. Session or database?
- 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
- http://www.asp.net/mvc/tutorials/security/using-oauth-providers-with-mvc (Using OAuth Providers with MVC 4 : The Official Microsoft ASP.NET Site)
- http://www.beabigrockstar.com/google-signin-for-asp-net-mvc/ (Google+ Signin for ASP.NET MVC - Be A Big Rockstar)
- http://coding.anyun.tw/2012/03/14/asp-net-mvc-using-oauth-2-0-connect-google-api/ (【oAuth 2.0 實作系列】ASP.Net MVC 實作使用 oAuth 2.0 連接 Google API « Coding 之路,不由分說)
- https://code.google.com/p/google-api-dotnet-client/ (google-api-dotnet-client - Google APIs Client Library for .NET - Google Project Hosting)
Github repository
As always, all source code can be found in Github in the repository
https://github.com/cage1016/ASPNETMvc4GoogleOAuth
https://github.com/cage1016/ASPNETMvc4GoogleOAuth
work like a charm.
ReplyDeleteAwesome solution. simple and did the job.
This comment has been removed by the author.
ReplyDeleteyour github solution was worked.but does not create a event and not sync my google calender
ReplyDeleteNot suitable for me because your github solution was worked.but does not create a event and not sync my google calender
ReplyDeleteThanks for provide great informatic and looking beautiful blog
ReplyDeletepython training in bangalore | python online training
aws online training in bangalore | aws online training
artificial intelligence training in bangalore | artificial intelligence online training
machine learning training in bangalore | machine learning online training
data science training in bangalore | data science online training
The content of this website was really informative. 50 High Quality Backlinks for just 50 INR
ReplyDelete2000 Backlink at cheapest
5000 Backlink at cheapest
Boost DA upto 15+ at cheapest
Boost DA upto 25+ at cheapest
Boost DA upto 35+ at cheapest
Boost DA upto 45+ at cheapest
Annabelle loves to write and has been doing so for many years.Cheapest and Fastest Link Building and Link Indexing Service TECKUM IS ALL ABOUT TECH NEWS AND MOBILE REVIEWS. Best GPL Store
ReplyDeleteAnnabelle loves to write and has been doing so for many years.Cheapest and fastest Backlink Indexing Best GPL Store TECKUM IS ALL ABOUT TECH NEWS AND MOBILE REVIEWS
ReplyDeleteGreat blog,
ReplyDeleteDigital Marketing Course in Hyderabad
Impressive. Your story always bring hope and new energy. Keep up the good work.
ReplyDeleteofficialPanda
This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post.
ReplyDeletecyber security certification malaysia