0

Google認証を使用してユーザーを認証しようとしています。認証する必要がありますが、ログイン後にユーザーをリダイレクトする方法とログアウトする方法がわかりません。Oauth 2を使用するGoogle

Googleドキュメント(https://developers.google.com/identity/sign-in/web/sign-in)を使用しようとしています。しかし、私はエラーがあります:Cannot read property 'getAuthInstance' of undefined。ここで

はコードです:

<script src="https://apis.google.com/js/platform.js" async defer></script> 
<script> 
    var redirectUri = 'http://localhost:4200/list'; 
    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. 
    } 
    function signOut() { 
     var auth2 = gapi.auth2.getAuthInstance(); 
     auth2.signOut().then(function() { 
      console.log('User signed out.'); 
     }); 
    } 
</script> 

誰もがそれを修正する方法を知っていますか?

答えて

0

スクリプト内でgapi.auth2.init()メソッドを使用するか、を<head>に含める必要があります。

あなたのコードは次のようになります

<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> 
<script> 
    var redirectUri = 'http://localhost:4200/list'; 
    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. 
    } 
    function signOut() { 
     var auth2 = gapi.auth2.getAuthInstance(); 
     auth2.signOut().then(function() { 
      console.log('User signed out.'); 
     }); 
    } 
</script> 
gapi.auth2.init()法上の

詳細情報:https://developers.google.com/identity/sign-in/web/reference#gapiauth2initwzxhzdk19paramswzxhzdk20

関連する問題