2017-08-11 39 views
0

堆棧溢出的Hello Peoples, 我正在開發一種集成Google-Sign in到我的web應用程序的方式。這是代碼如何在JavaScript中添加Google登錄?

<script src="https://apis.google.com/js/platform.js" async defer></script> 
<meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com"> 
<div class="g-signin2" data-onsuccess="onSignIn"></div> 
<script> 
function onSignIn(googleUser) { 
    var profile = googleUser.getBasicProfile(); 
    console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead. 
    console.log('Name: ' + profile.getName()); 
    console.log('Image URL: ' + profile.getImageUrl()); 
    console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present. 
} 

幫助

回答

0

你可以試試下面的代碼,你也可以參考https://developers.google.com/identity/sign-in/web/sign-in有關每個功能的詳細信息。

<html lang="en"> 
    <head> 
    <meta name="google-signin-scope" content="profile email"> 
    <meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com"> 
    <script src="https://apis.google.com/js/platform.js" async defer></script> 
    </head> 
    <body> 
    <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div> 
    <script> 
     function onSignIn(googleUser) { 
     // Useful data for your client-side scripts: 
     var profile = googleUser.getBasicProfile(); 
     console.log("ID: " + profile.getId()); // Don't send this directly to your server! 
     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()); 

     // The ID token you need to pass to your backend: 
     var id_token = googleUser.getAuthResponse().id_token; 
     console.log("ID Token: " + id_token); 
     }; 
    </script> 
    </body> 
</html> 
相關問題