(PHP)Gmail第三方登录

Gmail第三方登录

在开发Gmail第三方登录前 先通过google开发者后台申请需要的client_id
然后复制以下代码 讲申请下来的id替换XXXXXXXXXXX

<!--  Gmail第三方登陆 -->
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="XXXXXXXXXXX">
<script src="https://apis.google.com/js/api:client.js?onload=startApp" async defer></script>
<script>
  var googleUser = {};
  window.startApp = function() {
    gapi.load('auth2', function(){
      // Retrieve the singleton for the GoogleAuth library and set up the client.
      auth2 = gapi.auth2.init({
        client_id: 'XXXXXXXXXXX',
        cookiepolicy: 'single_host_origin',
        // Request scopes in addition to 'profile' and 'email'
        //scope: 'additional_scope'
      });
      attachSignin(document.getElementById('customBtn'));
    });
  };

  function attachSignin(element) {

    auth2.attachClickHandler(element, {},
        function(googleUser) {
        
              var profile = googleUser.getBasicProfile();
              //整理gmail第三方登录之后返回的用户信息
              var myInfo = new Array()
              myInfo[0] = profile.getId();
              myInfo[1] = profile.getName();
              myInfo[2] = profile.getImageUrl();
              myInfo[3] = profile.getEmail();
             $.ajax({           
                    type:'POST',
                    url: "/ajax/fit.member_new.ajax.php",
                    data:{userdata:JSON.stringify(myInfo),type:'gmailsignin'},
                    success: function(data){
                          var data = JSON.parse(data);
                        art.dialog({
                            title:langs[lang]['DIALOG_TIPS'],
                            icon: data['result'],
                            content:data['intro'],
                            okVal:'ok'
                        });
                        
                     location.replace(location);
                    }
            });

        }, function(error) {
          console.log(JSON.stringify(error, undefined, 2));
        });
  }
 </script>
 <!--  Gmail第三方登陆END  -->

在登录成功后 会返回对应的数据 我们可以通过console.log在js中打印出来 然后整理需要的数据 通过ajax传递到后台进行分析处理。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容