OAuth2.0 message flow demonstration using a sample application
In this blog post we will be explaining the procedure
of adapting Oauth2.0 to invoke apis from GoogleApi from the sample application we
created called MyCalender.
The sample application is written in ASP.NET MVC and
it invokes Google Calender api and list down future events of the user.
We have implemented Authorization Code Grant flow in
oAuth2.0. We have used ASP.NET external authentication functionalities to implement
the OAuth flow.
We'll discuss step by step on adapting OAuth flow for our application.
Step 1 -
1. First, we need to obtain the Client ID
and the Client Secret from ‘Google Developer Console’ where we have created our
application named ‘CalenderOperations'.
d Step 2 -
1. Now we need to obtain an authorization token
before in order to obtain the access token.
In our application click on Login
button.
It loads the page where we have a button for ‘Google’
login. Click on that.
It will redirect our browser to Google Login page
where we need to enter our Google credentials.
At this point the url would be
We will extract the url elements and have look
In above url we do a get request to below url
We pass the below parameters with
that url
- client_id – We obtained this on step 1.
- response_type – Pass the value ‘code’ for this parameter. This requests the authorization code.
- Scope – Since we need to access calendar events from Google Calendar API we pass ‘CalendarReadonly’ scope.
- redirect_uri – For this we use the localhost domain of our application ‘’
Now we get a window asking us to give permissions to
application ‘CalenderOperations’ to access our protected data from Google
Calendar Api.
Now confirm the selection.
When you click on allow it will redirect to our localhost
‘http://localhost:60531/’.
Now we can extract the ‘Authorization Code’ from redirected
uri. We are ready to get the access token by exchanging this ‘Authorization Code’
with the google authorization server.
Step 3 -
1. Now we obtain the access token. The ASP.NET
server application passes the above obtained code, client_id, client_secret, grant_type
which the value needs to be ‘authorization_code’ and redirect_uri along with the
below url and obtains the access token.
If you check the google
account you will see that ‘CalendarOperations’ application has access to read
events from our google calendar.
Step 4 -
1.
Now we pass the above obtained access
token to Google calendar api and obtained the future events as below.
Thank you very much for taking the time to read this blog post! :)
Please find the github repository for the application here
https://github.com/Dhanushi12/MyCalendar.git
