Javascript 使用OAuth2Google

Javascript 使用OAuth2Google,javascript,angular,oauth,google-oauth,google-authentication,Javascript,Angular,Oauth,Google Oauth,Google Authentication,我正在尝试使用GoogleAPI对用户进行身份验证。我必须进行身份验证,但我不知道在登录后如何重定向用户以及如何注销 我正在尝试使用谷歌文档:。但我有一个错误:无法读取未定义的的属性“getAuthInstance” 代码如下: <script src="https://apis.google.com/js/platform.js" async defer></script> <script> var redirectUri = 'http://loc

我正在尝试使用GoogleAPI对用户进行身份验证。我必须进行身份验证,但我不知道在登录后如何重定向用户以及如何注销

我正在尝试使用谷歌文档:。但我有一个错误:
无法读取未定义的
的属性“getAuthInstance”

代码如下:

<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>

var重定向URI=http://localhost:4200/list';
函数onSignIn(谷歌用户){
var profile=googleUser.getBasicProfile();
console.log('ID:'+profile.getId());//不要发送到后端!改用ID令牌。
log('Name:'+profile.getName());
log('Image URL:'+profile.getImageUrl());
console.log('Email:'+profile.getEmail());//如果“Email”作用域不存在,则此值为null。
}
函数签出(){
var auth2=gapi.auth2.getAuthInstance();
auth2.signOut().then(函数(){
log('User signed out');
});
}

有人知道如何修复它吗?

您需要在脚本中使用
gapi.auth2.init()
方法,或者在
中使用

您的代码如下所示:

<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>

var重定向URI=http://localhost:4200/list';
函数onSignIn(谷歌用户){
var profile=googleUser.getBasicProfile();
console.log('ID:'+profile.getId());//不要发送到后端!改用ID令牌。
log('Name:'+profile.getName());
log('Image URL:'+profile.getImageUrl());
console.log('Email:'+profile.getEmail());//如果“Email”作用域不存在,则此值为null。
}
函数签出(){
var auth2=gapi.auth2.getAuthInstance();
auth2.signOut().then(函数(){
log('User signed out');
});
}
有关
gapi.auth2.init()方法的详细信息: