当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Web网站实现Google登录

Web网站实现Google登录

2019年07月02日  | 移动技术网IT编程  | 我要评论

一、打开谷歌控制台:https://console.developers.google.com/apis

二、点击创建凭据,如下图,填写项目地址等

三、创建好客户端id和秘钥后,填写对应的项目网址和登录页网址

四、修改oauth同意屏幕网站首页地址和隐私政策网址

五、代码部分如下:

<script src="https:///js/api:client.js"></script>function google_login() {
        var googleuser = {};
            gapi.load('auth2', function(){
                // retrieve the singleton for the googleauth library and set up the client.
                auth2 = gapi.auth2.init({
                    client_id: '申请得到的客户端id', //客户端id
                    cookiepolicy: 'single_host_origin',
                    scope: 'profile' //可以请求除了默认的'profile' and 'email'之外的数据
                });
                attachsignin(document.getelementbyid('google_button')); //点击google登录的按钮
            });

    }

    function attachsignin(element) {
        auth2.attachclickhandler(element, {},
            function(googleuser) {
                var profile = auth2.currentuser.get().getbasicprofile();
                console.log('id: ' + profile.getid());
                console.log('full name: ' + profile.getname());
                console.log('given name: ' + profile.getgivenname());
                console.log('family name: ' + profile.getfamilyname());
                console.log('image url: ' + profile.getimageurl());
                console.log('email: ' + profile.getemail());
            }, function(error) {
                console.log(json.stringify(error, undefined, 2));
            });
    }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网