Get email ID/username after login using Google+ API

9.5k Views Asked by At

I want to access the user_name/email_id of the user who logs onto my website using Google+ API. So far I have implemented the Google+ API and the return value is:

User Logged In This is his auth tokenya29.AHES6ZRWhuwSAFjsK9jYQ2ZA73jw9Yy_O2zKjmzxXOI8tT6Y

How can I use this to get the username/email id?

3

There are 3 best solutions below

0
On

if you are correctly logged in, it's enough to call the Google+ api at this URL:

GET https://www.googleapis.com/plus/v1/people/me

where the userId has the special value me, to get all the information about the logged user. For more information see: https://developers.google.com/+/api/latest/people/get

0
On

Specifically for retrieving an email address of an authenticated user, keep in mind that you will need to include the userinfo.email scope and make a call to the tokeninfo endpoint. For more information on this, see https://developers.google.com/+/api/oauth#scopes.

0
On

I'm adding a code sample to help others.

In this case the login operation is performed against Google requesting email, as well as user profile information like name,.... Once all this information is retrieved, a request to my own login service is performed:

        function OnGoogle_Login(authResult) {
                    if (authResult['access_token']) {
                        gapi.client.load('oauth2', 'v2', function()
                        {
                            gapi.client.oauth2.userinfo.get().execute(function(userData)
                            {
                                $("#frmLoginGoogle input[name='id']").val(userData.id);
                                $("#frmLoginGoogle input[name='name']").val(userData.name);
                                $("#frmLoginGoogle input[name='email']").val(userData.email);
                                $.ajaxSetup({cache: false});
                                $("#frmLoginGoogle").submit();
                            });
                        });
                    }
                }

        $(document).ready(function() {

            /** GOOGLE API INITIALIZATION **/
            $.ajaxSetup({cache: true});

            $.getScript("https://apis.google.com/js/client:platform.js", function() {
                            $('#btnLoginGoogle').removeAttr('disabled');
                        });

            $("#btnLoginGoogle").click(function() {
                        gapi.auth.signIn({
                            'callback': OnGoogle_Login,
                            'approvalprompt': 'force',
                            'clientid': 'XXXXX.apps.googleusercontent.com',
                            'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email',
                            'requestvisibleactions': '',
                            'cookiepolicy': 'single_host_origin'
                        });
                    });
        });