Google API授权不再适用于Firebase

时间:2017-08-02 13:50:13

标签: javascript firebase google-api firebase-authentication

我正在使用Firebase处理应用程序(在React中,但这与我猜的问题并不特别相关)。

很简单,客户端加载谷歌API脚本



  ...
  <script src="https://apis.google.com/js/api.js"></script>
</head>
&#13;
&#13;
&#13;

然后我将此功能用于&#34; init&#34;它:

&#13;
&#13;
function initGoogleAPI() {
    gapi = window.gapi;

    function initClient() {
        // 2. Initialize the JavaScript client library.
        gapi.client
            .init({
                apiKey: "----------_API_KEY_--------- ",
                clientId:
                    "------_CLIENT_ID_-------.apps.googleusercontent.com", //config.google.client_id,
                discoveryDocs: [
                    "https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"
                ],
                scope: "https://www.googleapis.com/auth/gmail.readonly" // from https://developers.google.com/identity/protocols/googlescopes
            })
            .then(() => {
                store.state.currentUserProfile = null;
                store.state.authenticated = false;

                // Listen for sign-in state changes.
                gapi.auth2
                    .getAuthInstance()
                    .isSignedIn.listen(updateAuthStatus);

                // Handle the initial sign-in state.
                    updateAuthStatus(gapi.auth2.getAuthInstance().isSignedIn.get());

            });
    }

    // 1. Load the JavaScript client library.
    gapi.load("client:auth2", initClient);
}
&#13;
&#13;
&#13;

这会触发updateAuthStatus()函数。这个函数做了一些二次检查和设置,但基本上它现在破坏了signiInWithCredential方法调用:

&#13;
&#13;
function updateAuthStatus() {
    return new Promise(resolve => {
        // we have no auth!
        ...

        var googleUser = gapi.auth2.getAuthInstance().currentUser.get();
        var googleProfile = googleUser.getBasicProfile();
        var isSignedIn = gapi.auth2.getAuthInstance().isSignedIn.get();

        if (isSignedIn) {
            var credential = firebase.auth.GoogleAuthProvider.credential(
                googleUser.getAuthResponse().id_token
            );
            // we now want to auth in to Firebase with the googleUser credentials
            firebase.auth().signInWithCredential(credential).then(user => {
                // do we have a user?
                var hasUser = Boolean(user !== null);

                if (hasUser) {
                    DB.ref("users").child(user.uid).once("value", snap => {
                        var profile = snap.val();
                        console.log(user);
                        if (profile === null) {
                            var payload = {
                                email: user.email,
                                displayName: user.displayName,
                                photoURL: user.photoURL,
                                refreshToken: user.refreshToken,
                                signupDate: new Date().getTime(),
                                uid: user.uid
                            };
                            snap.ref.update(payload).then(e => {
                                store.state.currentUserProfile = payload;
                                store.state.authenticated = hasUser;
                                resolve();
                            });
                        } else {
                            store.state.currentUserProfile = profile;
                            store.state.authenticated = hasUser;
                            resolve();
                        }
                    });
                } else {
                    resolve(hasUser);
                }
            });
            
        } else {
            store.state.authenticated = false;
            resolve(false);
        }
    });
}
&#13;
&#13;
&#13;

问题是,当我调用signInWithCredential方法时,我现在收到此错误:

  

错误:   {&#34;错误&#34; {&#34;错误&#34;:[{&#34;结构域&#34;:&#34; usageLimits&#34;&#34;理由&#34;:& #34; keyExpired&#34;&#34;消息&#34;:&#34;差   请求&#34;}],&#34;代码&#34;:400,&#34;消息&#34;:&#34;错误请求&#34;}}

我也担心它可能与Firebase / Google Cloud APP密钥有关。现在,我认为两种服务之间的界面和集成非常混乱。

同样的代码正在运行并停止了。你会如何调试这个?

0 个答案:

没有答案