Search code examples
javascriptoauth-2.0google-apigoogle-oauth

Google OAuth resulting in server_error


When my window loads I run the following code:

gapi.load("client:auth2", () => {
    gapi.auth2.init({
        client_id: "MY_CLIENT_ID.apps.googleusercontent.com"
    });
});

After the initialization completes and after the user clicks a sign-in button, I run the following code:

gapi.auth2.getAuthInstance()
    .signIn({ scope: "https://www.googleapis.com/auth/youtube.force-ssl" })
    .then((res) => {
        console.log("Sign-in successful");
        console.log(res);
    })
    .catch((err) => {
        console.error("Error signing in");
        console.error(err);
    })
;

I get the following error in my console:

error: "server_error"

enter image description here


How do I fix this?



Solution

  • This code worked for me:

    HTML

    <script src="https://apis.google.com/js/platform.js"></script>
    <script src="https://apis.google.com/js/client.js"></script>
    

    JAVASCRIPT

    await gapi.client.init({
        clientId: "CLIENT_ID.apps.googleusercontent.com",
        scope: "https://www.googleapis.com/auth/INSERT_SCOPE_HERE",
    });
    
    try {
        res = await gapi.auth2.getAuthInstance().signIn();
    } catch (err) {
        console.error("Error signing in");
        console.error(err);
        return;
    }
            
    console.log("Sign-in successful");
    console.log(res);