简体   繁体   English

Google 网站登录,获取用户性别

[英]Google Sign-In for Websites, get user gender

I am trying to build social logins for a client's site.我正在尝试为客户的网站建立社交登录。 I've done Facebook and I'm now trying to get Google to work.我已经完成了 Facebook,现在我正在努力让 Google 工作。

I have the following:我有以下几点:

$(document).ready(function() {
    $.ajaxSetup({ cache: true });
    $.getScript('//apis.google.com/js/platform.js', function(){
        gapi.load('auth2', function() {
            auth2 = gapi.auth2.init({
                client_id: '(MY_CLIENT_ID_HERE).apps.googleusercontent.com',
                scope: 'profile',
            });
            // basic listeners
        });
    });
});

I then check if the user is logged in and get the current user using var googleUser = auth2.currentUser.get() , and the user's basic profile info with var profile = googleUser.getBasicProfile() .然后我检查,如果用户登录并使用获得当前用户var googleUser = auth2.currentUser.get()并得到了用户的基本个人资料信息var profile = googleUser.getBasicProfile() So far so good.到目前为止一切顺利。

I can read basic profile values like:我可以读取基本的配置文件值,例如:

console.log('First Name: ' + profile.getGivenName());
console.log('Last Name: ' + profile.getFamilyName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());

Great!太棒了!

The one piece of information I get by default from Facebook that Google is not returning is the user's gender .默认情况下,我从 Facebook 获得的 Google 没有返回的一条信息是用户的性别

I've tried changing the scope in the object provided to gapi.auth2.init but could not find a way to retrieve the user's gender.我尝试更改提供给gapi.auth2.init的对象中的scope ,但找不到检索用户性别的方法。

Can anyone show me a working example?任何人都可以向我展示一个工作示例吗? Or point me in the right direction?或者指出我正确的方向?

Thanks!谢谢!

scopes: [
  'profile',
  'https://www.googleapis.com/auth/user.gender.read',
],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM