使用Google+ API登录后获取电子邮件ID /用户名

时间:2013-08-26 04:47:05

标签: google-app-engine google-plus google-openid

我想访问使用Google+ API登录我网站的用户的user_name / email_id。到目前为止,我已经实施了Google+ API,返回值为:

User Logged In This is his auth tokenya29.AHES6ZRWhuwSAFjsK9jYQ2ZA73jw9Yy_O2zKjmzxXOI8tT6Y

我如何使用它来获取用户名/电子邮件ID?

3 个答案:

答案 0 :(得分:2)

专门用于检索经过身份验证的用户的电子邮件地址,请记住,您需要包含userinfo.email范围并调用tokeninfo端点。有关详细信息,请参阅https://developers.google.com/+/api/oauth#scopes

答案 1 :(得分:1)

如果您已正确登录,只需通过以下网址致电Google+ api即可:

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

userId具有特殊值me,以获取有关已登录用户的所有信息。有关更多信息,请参阅 https://developers.google.com/+/api/latest/people/get

答案 2 :(得分:0)

我正在添加代码示例以帮助其他人。

在这种情况下,登录操作是针对Google请求电子邮件以及用户个人资料信息(如姓名等)执行的....一旦检索到所有这些信息,就会执行对我自己的登录服务的请求:

        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'
                        });
                    });
        });
相关问题