Google APIs Auth Simple 2

Please Share

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.

Please Share

Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as a developer and a database expert. I have been working with several of the Google APIs, since 2012. I helping others in the On-Line community to develop with the Google APIs, by creating my own blog This and my presence on a number of On-Line developer forums lead me to be noticed by the Google Analytics API development team. I was nominated for and recently became one of the first Google Developer Experts for Google Analytics.

Leave a comment

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

2 thoughts on “Google APIs Auth Simple

  • Richard Mikesell

    Linda, thanks for these code samples, they are very helpful. I have been struggling with “Step Two” – exchanging the authorization code for the access and refresh tokens. (I get the authorization code from the client/js login code, which works perfectly. But whether I run it on my localhost or test site, I always get Error 400 (Bad Request) at the “dataStream = response.GetResponseStream()” line. Can you suggest any reasons why I am consistently getting this error? Thank you!