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 26, 2018, 11:26 PM EDT
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, 2:01 AM EST
Thank you so much.Nice article..
Ashok KeshriPosted Apr 20, 2016, 11:00 PM EDT
can u solve my problem
Ashok KeshriPosted Apr 20, 2016, 11:00 PM EDT
Messsage "Invalid Credentials"
lakshman kumarPosted Oct 16, 2015, 5:13 AM EDT
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, 11:52 AM EDT
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, 5:56 AM EDT
Please can you give the response as early as possible.
Vishwakant TripathiPosted Oct 15, 2015, 2:06 AM EDT
@lakshman, just give me some time, I will check it and try to find out for you.
lakshman kumarPosted Oct 14, 2015, 6:08 AM EDT
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 23, 2015, 9:40 PM EDT
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, 12:17 AM EDT
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 2, 2015, 8:06 PM EDT
hello how can I get the userID
Rajan PandeyPosted Jun 2, 2015, 2:45 AM EDT
how do we get user email and gender in this code..
Suresh MogudalaPosted May 25, 2015, 12:34 AM EDT
Thanks, will please tell who to validate each asp form, where there User is logged in or not ?
Manoj KulkarniPosted May 9, 2015, 1:48 AM EDT
Nice
Vivi VPosted May 8, 2015, 5:22 AM EDT
After authorizing the app, i get the foll error:This webpage is not available ERR_CONNECTION_REFUSED
Itdevp DevpPosted Apr 18, 2015, 4:24 AM EDT
After Click Accept Button Close the popup window but not call Redirect page .
Itdevp DevpPosted Apr 18, 2015, 2:22 AM EDT
After login 4 user it will not work for me.
Itdevp DevpPosted Apr 18, 2015, 2:20 AM EDT
No of User limitation
Gowtham RajamanickamTOP 100Posted Apr 16, 2015, 1:17 AM EDT
good one...kepp going
Monibrata BhattacharjeePosted Apr 15, 2015, 6:50 AM EDT
Really Nice one...Thanks Viswa...keep it up for us atleast... :)
Karthik Muthu KaruppanPosted Apr 13, 2015, 9:33 AM EDT
nice one
Shubham KumarPosted Apr 13, 2015, 5:44 AM EDT
nice
Siddharth SPosted Apr 13, 2015, 2:18 AM EDT
yeah, it's working for me. thnks.
NitinPosted Apr 12, 2015, 10:38 PM EDT
well explained
Sibeesh VenuTOP 100Posted Apr 12, 2015, 9:57 PM EDT
Good one :)
Santhakumar MunuswamyPosted Apr 12, 2015, 7:09 PM EDT
Good Start
Siddharth SPosted Apr 7, 2015, 9:46 AM EDT
I am not able to download attached sample file, is it able to download to anyone?
Gowtham RajamanickamTOP 100Posted Apr 7, 2015, 6:56 AM EDT
good start
Vivi VPosted Apr 6, 2015, 5:10 AM EDT
Can't retrieve the user details. Someone please help
hiral prajapatiPosted Mar 30, 2015, 10:38 PM EDT
its not working
Akhil MittalPosted Sep 5, 2014, 4:45 AM EDT
Its not working
Dinesh BeniwalTOP 10Posted Aug 5, 2013, 6:42 PM EDT
Good Start, Welcome to the C# Corner Tripathi.