Login using Google API with C#


Introduction


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

Description

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 available option.
Select application type

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

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

Prerequiste:

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.

Conclusion

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.

No comments:

Post a Comment

Dynamic Query in LINQ using Predicate Builder

Introduction This tutorial explains how to create a dynamic query using LINQ, Using Predicate Builder  LINQ to SQL dynamic query an...

Learn More