2017-05-30 73 views
1

我一直停留在這幾個小時。我試過谷歌搜索答案,但我不明白其中的任何和必須假設它是過時的代碼。谷歌登錄 - 防止自動負載

我想添加一個'用google登錄'到我的網站,但我希望它只能重定向到登錄檢查,如果用戶點擊按鈕,而不是一旦它檢測到我已經登錄在

按鈕:

<div class="g-signin2" data-onsuccess="onSignIn"></div> 

腳本:

<script> 
function onSignIn(googleUser) { 
    var profile = googleUser.getBasicProfile(); 
    console.log('ID: ' + profile.getId()); 
    console.log('Name: ' + profile.getName()); 
    var id_token = googleUser.getAuthResponse().id_token; 
    window.location.replace("googleLogin.phptoken="+ id_token); 
    } 
</script> 

任何人都征服了這個問題?

回答

0

我真的不能記得我做過什麼來解決這個問題,但我有同樣的一個最終使這裏一個新的腳本是一個js撥弄一個鏈接,你將有自己的客戶編號堵塞,它不有一個圖像,所以你只會看到谷歌登錄按鈕的一張破碎的圖片。

var googleUser = {}; 
var startApp = function() { 
gapi.load('auth2', function(){ 
    // Retrieve the singleton for the GoogleAuth library and set up the client. 
    auth2 = gapi.auth2.init({ 
    client_id: '83******98-pe4uneu******sj.apps.googleusercontent.com', 
    cookiepolicy: 'single_host_origin', 
    // Request scopes in addition to 'profile' and 'email' 
    //scope: 'additional_scope' 
    }); 
    attachSignin(document.getElementById('customBtn')); 
}); 
}; 

function attachSignin(element) { 
console.log(element.id); 
auth2.attachClickHandler(element, {}, 
    function(googleUser) { 
    first_name = googleUser.getBasicProfile().getGivenName(); 
     last_name = googleUser.getBasicProfile().getFamilyName(); 
     email = googleUser.getBasicProfile().getEmail(); 
     console.log(googleUser.getBasicProfile().getGivenName()); 
     console.log(googleUser.getBasicProfile().getFamilyName()); 
     console.log(googleUser.getBasicProfile().getEmail()); 
     window.location = "/profile"  
    }, function(error) { 
     alert(JSON.stringify(error, undefined, 2)); 
    }); 

} 

startApp(); 

https://jsfiddle.net/josephmckenzie/9pxc41kx/

所有你可能需要的代碼是在爵士小提琴