美文网首页
(PHP)Gmail第三方登录

(PHP)Gmail第三方登录

作者: HueyYao | 来源:发表于2020-11-21 21:33 被阅读0次

    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传递到后台进行分析处理。

    相关文章

      网友评论

          本文标题:(PHP)Gmail第三方登录

          本文链接:https://www.haomeiwen.com/subject/jxvobktx.html