GoogleAuth javascript API未检测到已登录的用户

时间:2018-08-07 03:03:30

标签: javascript api google-oauth

我正在使用Google Javascript API,它可以直接登录,尽管它无法检测到页面加载时已经登录的用户。 这是我的代码:

loadGoogleAPI() {
        const script = document.createElement("script");
        let $this = this;
        script.onload = () => {
            gapi.load('client:auth2', () => {
                gapi.client.init({
                    apiKey: '',
                    clientId: '',
                    scope: 'https://www.googleapis.com/auth/classroom.courses profile',
                    accessType: 'offline'
                }).then(() => {
                   let auth2 = gapi.auth2.getAuthInstance();
                   console.log(auth2);
                    auth2.currentUser.listen((val) => {
                        this.setState({auth2: this.state.auth2});
                        console.log(val)
                    });
                    if (auth2.isSignedIn.get()) {
                        console.log(1);
                        this.state.auth2.signIn();
                    }
                    auth2.attachClickHandler(Style.parse('sign-in-button'), {}, function(user) {
                        console.log(user);
                    }, function (err) {
                        console.log(err);
                    });
                    this.setState({auth2})
                });
            });
        };
        script.src = "https://apis.google.com/js/platform.js";
        document.head.appendChild(script);
    }

0 个答案:

没有答案