Google APIs Auth Simple 6

Google APIsAre you trying to access one or more of the Google APIs?  Are you unable for some reason to use Googles official .net client library?   I have a possible solution for you.  In this tutorial i am going to share with you a class i called Google Auths simple.  It is just that a simple set of classes that will allow you to authenticate to simply authenticate a user using OAuth2 to google and get a access token back.    Once you have your access token you can access any of the Google APIs.

Oauth2 step one the consent screen.

The initial URL to request that the user give you access to there account should look like this.{clientid}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope={scopes}&response_type=code
  • The  {clientid} is found on the Google developers console.   If you don’t know how to create that you can read my Google developers for beginners tutorial series specifically the OAuth2 tutorial.   Google Developer Console Oauth2 credentials.
  • {scopes} is a comma separated string representing the permissions that you will need.   Each API has a different set of scopes available.   Each method with in the API may require different scopes.    For example:  If i want to add events to a users Google Calendar, asking for only read only permission will not allow me to do this I will need to ask for the permission to add events.

This URL is just a HTTP GET so you can place the URL in a browser window.  If you open that link in a browser you will see this.:

Step one and a half

When the user clicks accept an authentication code is returned to you. How you get that code is up to you. I normally place the uri in a web browser control then grab it from there.

Step two exchange the authentication code.

The Authentication code is only used once. It is used to get an Access token and a Refresh token. The access token is what you will use to access the Google API, access tokens expire after one hour.


step three refresh access token

After an hour has gone by the access token will expire you will need to request a new access token. We use the refresh token to get a new access token. You should save the refresh token some place this is what will enable you to access the users account again with out asking them for permission.


You can use the access token with the API whose scope you requested access for by simply placing access_token= on the end of the request.{yourAccessToken}

Wrap up

We cant always use the Google .net client library in every project. Google Auth simple is a quick and dirty way of enabling Google Authentication in your application.

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 thoughts on “Google APIs Auth Simple