How to get Google API access:
- Open https://code.google.com/apis/console/?pli=1
- Login using your Gmail account.
- Click on "Credentials" under "APIs & auth" in the left side.

- Click the "Create new client ID" button.

- It will open a dialog box to create a new Client ID.

- Select Web Application and Set the Authorized JavaScript origins (example: http://localhost), Authorized redirect URIs (example: http://localhost/googleplus/Test.aspx).
- Click "Create Client ID" and it will create a Client ID detail.

- Redirect URIs and Client ID need to replaced in the Test.aspx file.
ASP.Net application
In the code below, first set your Client ID and Redirect URI. I have provided an example to show the logged in username and profile image, you can use it in any other way also. In the function getUserInfo(), you can get the results as username, email and and so on.
jQuery Link: jQuery.js
- <HTML>
- <Head>
- <script src="jquery.js" temp_src="jquery.js" type="text/javascript"></script>
- <script language="javascript" type="text/javascript">
- var OAUTHURL = 'https://accounts.google.com/o/oauth2/auth?';
- var VALIDURL = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=';
- var SCOPE = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email';
- var CLIENTID = '132264420893-na047nu1sif8dn1p6qaug895feu2lqbp.apps.googleusercontent.com';
- var REDIRECT = 'http://localhost/googleplus/Test.aspx';
- var LOGOUT = 'http://accounts.google.com/Logout';
- var TYPE = 'token';
- var _url = OAUTHURL + 'scope=' + SCOPE + '&client_id=' + CLIENTID + '&redirect_uri=' + REDIRECT + '&response_type=' + TYPE;
- var acToken;
- var tokenType;
- var expiresIn;
- var user;
- var loggedIn = false;
- function login()
- {
- var win = window.open(_url, "windowname1", 'width=800, height=600');
- var pollTimer = window.setInterval(function () {
- try
- {
- console.log(win.document.URL);
- if (win.document.URL.indexOf(REDIRECT) != -1)
- {
- window.clearInterval(pollTimer);
- var url = win.document.URL;
- acToken = gup(url, 'access_token');
- tokenType = gup(url, 'token_type');
- expiresIn = gup(url, 'expires_in');
- win.close();
- validateToken(acToken);
- }
- }
- catch (e)
- {
- }
- }, 500);
- }
- function validateToken(token)
- {
- $.ajax(
- {
- url: VALIDURL + token,
- data: null,
- success: function (responseText)
- {
- getUserInfo();
- loggedIn = true;
- $('#loginText').hide();
- $('#logoutText').show();
- },
- dataType: "jsonp"
- });
- }
- function getUserInfo()
- {
- $.ajax({
- url: 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + acToken,
- data: null,
- success: function (resp)
- {
- user = resp;
- console.log(user);
- $('#uName').text('Welcome ' + user.name);
- $('#imgHolder').attr('src', user.picture);
- },
- dataType: "jsonp"
- });
- }
- //credits: http://www.netlobo.com/url_query_string_javascript.html
- function gup(url, name)
- {
- namename = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
- var regexS = "[\\#&]" + name + "=([^]*)";
- var regex = new RegExp(regexS);
- var results = regex.exec(url);
- if (results == null)
- return "";
- else
- return results[1];
- }
- function startLogoutPolling()
- {
- $('#loginText').show();
- $('#logoutText').hide();
- loggedIn = false;
- $('#uName').text('Welcome ');
- $('#imgHolder').attr('src', 'none.jpg');
- }
- </script>
- </Head>
- <body>
- <a href='#' onClick='login();' id="loginText"'> Google Plus </a>
- <a href="#" temp_href="#" style="display:none" id="logoutText" target='myIFrame' onclick="myIFrame.location='https://www.google.com/accounts/Logout'; startLogoutPolling();return false;"> Click here to logout </a>
- <iframe name='myIFrame' id="myIFrame" style='display:none'></iframe>
- <div id='uName'></div>
- <img src='' id='imgHolder'/>
- </body>
- </HTML>

Tengku FauzanPosted Jun 27, 2018, 3:26 AM
Hi, Can u also do some tutorial on google+ in wcf service which be able to authenticate the user and get some profile information using windows form/WPF to authenticate and view user info. it would be a great help for me. Using C#
Sagar Pandurang KapPosted Dec 14, 2017, 7:01 AM
Thank you so much.Nice article..
Ashok KeshriPosted Apr 21, 2016, 3:00 AM
can u solve my problem
Ashok KeshriPosted Apr 21, 2016, 3:00 AM
Messsage "Invalid Credentials"
lakshman kumarPosted Oct 16, 2015, 9:13 AM
Hi Tripati As of your above post we get only mail id,I also want to retrieve user inbox to my application. How to do that.
Jyotirmay GhoshPosted Oct 15, 2015, 3:52 PM
hello sir, i have tried to run this application. I got an error on it..Method not found: 'Void System.Web.UI.ScriptResourceDefinition.set_LoadSuccessExpression(System.String)'.
lakshman kumarPosted Oct 15, 2015, 9:56 AM
Please can you give the response as early as possible.
Vishwakant TripathiPosted Oct 15, 2015, 6:06 AM
@lakshman, just give me some time, I will check it and try to find out for you.
lakshman kumarPosted Oct 14, 2015, 10:08 AM
Hi It is very good article.But using this i am able to get only profile pic not there mails and contacts.Please Tell me the steps for getting user mails and contacts to our application.
soma navinPosted Sep 24, 2015, 1:40 AM
Hi VD tripathi...Now i getting error like ""HTTP Error 404.0 - Not FoundThe resource you are looking for has been removed, had its name changed, or is temporarily unavailable.""...Can u Please Help Me with these....Error
Prajakta RamdasiPosted Sep 18, 2015, 4:17 AM
Getting access denied error in javascript at console.log(win.document.URL); if (win.document.URL.indexOf(REDIRECT) != -1) { window.clearInterval(pollTimer); ---------------------------------------------------- Exception was thrown at line 29, column 17 in http://localhost:49265/Test.aspx 0x80070005 - JavaScript runtime error: Access is denied. If there is a handler for this exception, the program may be safely continued.
Alfred JosePosted Jul 3, 2015, 12:06 AM
hello how can I get the userID
Rajan PandeyPosted Jun 2, 2015, 6:45 AM
how do we get user email and gender in this code..
Suresh MogudalaPosted May 25, 2015, 4:34 AM
Thanks, will please tell who to validate each asp form, where there User is logged in or not ?
Manoj KulkarniPosted May 9, 2015, 5:48 AM
Nice
Vivi VPosted May 8, 2015, 9:22 AM
After authorizing the app, i get the foll error:This webpage is not available ERR_CONNECTION_REFUSED
Itdevp DevpPosted Apr 18, 2015, 8:24 AM
After Click Accept Button Close the popup window but not call Redirect page .
Itdevp DevpPosted Apr 18, 2015, 6:22 AM
After login 4 user it will not work for me.
Itdevp DevpPosted Apr 18, 2015, 6:20 AM
No of User limitation
Gowtham RajamanickamPosted Apr 16, 2015, 5:17 AM
good one...kepp going
Monibrata BhattacharjeePosted Apr 15, 2015, 10:50 AM
Really Nice one...Thanks Viswa...keep it up for us atleast... :)
Karthik Muthu KaruppanPosted Apr 13, 2015, 1:33 PM
nice one
Shubham KumarPosted Apr 13, 2015, 9:44 AM
nice
Siddharth SPosted Apr 13, 2015, 6:18 AM
yeah, it's working for me. thnks.
NitinPosted Apr 13, 2015, 2:38 AM
well explained
Sibeesh VenuPosted Apr 13, 2015, 1:57 AM
Good one :)
Santhakumar MunuswamyPosted Apr 12, 2015, 11:09 PM
Good Start
Siddharth SPosted Apr 7, 2015, 1:46 PM
I am not able to download attached sample file, is it able to download to anyone?
Gowtham RajamanickamPosted Apr 7, 2015, 10:56 AM
good start
Vivi VPosted Apr 6, 2015, 9:10 AM
Can't retrieve the user details. Someone please help
hiral prajapatiPosted Mar 31, 2015, 2:38 AM
its not working
Akhil MittalPosted Sep 5, 2014, 8:45 AM
Its not working
Dinesh BeniwalPosted Aug 5, 2013, 10:42 PM
Good Start, Welcome to the C# Corner Tripathi.