Login using Google API with C#


In this article, we learn about how to login using Google OAuth 2.0 with C#(.NET) or Authenticate Google API in Dot Net.


As we know Google OAuth 2.0 requires ClientId and ClientSecret before we proceeding to further step. So, first of all, we have to generate ClientId and ClientSecret from Google Developer Console by creating credential or generating a credential for Web Application or from Native Application.

I hope you have google account already if not then first create one before proceeding further.

Step 1: Create Credential for OAuth App.
Select credential type OAuth ClientID
Step 2: Select appropriate application type from the available option, Here I have choosed "Other".
Select application type

Based on where we are using login with google account we can select an application type. Here I have used "other" which works for a console app, Web application hosted on localhost as well as on all domain custom as well as localhost.

Step 3: Copy and save generate ClientId and Client Secret
Save ClientId and Client Secret

Now, you have your ClientId and ClientSecret which will be used for authenticating with Google API using C#.

Once you have setup project you need Google API Reference you can install from Nuget


Install-Package Google.Apis -Version 1.32.1

Here is main logic for authenticating google API
public async Task<Usercredential> getUserCredential()
            UserCredential credential;
            string[] scopes = new string[] {  }; // view and manage your Google Analytics data

            //Read client id and client secret from json file

            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                       new ClientSecrets
                           ClientId = ConfigurationManager.AppSettings["ClientId"],
                           ClientSecret = ConfigurationManager.AppSettings["ClientSecret"]
                       }, scopes,
                "user", CancellationToken.None, new FileDataStore("Auth.Api.Store"));

            return credential;
Hi, here I have created one single method

  • Which grabs ClientId and Client Secret from Web.Config App settings.
  • Passes scopes as a parameter. You can request more detail if you want while requesting from the user
  • Used await and task feature for thread safe and as performance improvement and it is already supported by Google API Library.
When you put above code in your project and run the app it will redirect to Google Account page for validating your credential and request for granting permission to use your basic profile information for login purpose.

If you receive any error it means permission is not given or ClientId or Client Secret key is not valid.

Sample SourceCode For Authentication: https://github.com/anomepani/GoogleApiDemos 


In this article, we have learned about how to create ClientId and Client Secret for a talk with Google API and Login with Google Account functionality using C#.Code.


Popular posts from this blog

Payumoney Integration With Asp.Net

Dynamic Query in LINQ using Predicate Builder