使用 javascript 登录 google 时出现错误

问题描述 投票:0回答:1

当我尝试通过谷歌登录时,我可以继续,但此时它显示空白强文本

    {% block content %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="referrer" content="strict-origin-when-cross-origin">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="google-signin-client_id" content="my client id">
    <title>Google Sign-In with JWT</title>
    <script src="https://accounts.google.com/gsi/client" async defer></script>
</head>
<body>
    <h1>Google Sign-In</h1>
    
    <div id="g_id_onload"
         data-client_id = "my client id"
         data-callback ="handleCredentialResponse"
         data-login-uri = "http://127.0.0.1:8000/user/login/"
         data-auto_prompt ="false">
    </div>
    
    <div class="g_id_signin"
         data-type="standard"
         data-size="large"
         data-theme="outline"
         data-text="sign_in_with"
         data-shape="rectangular"
         data-logo_alignment="left">
    </div>

    <script>
        console.log("Current origin:", window.location.origin);

        function handleCredentialResponse(response) {
            console.log("Received response from Google Sign-In");
            const idToken = response.credential;
            console.log("ID Token:", idToken);

            fetch('http://127.0.0.1:8000/user/login/', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-Requested-With': 'XMLHttpRequest'
                },
                body: JSON.stringify({ token: idToken }),
                credentials: 'include'
            })
            .then(response => {
                console.log("Received response from server");
                return response.json();
            })
            .then(data => {
                if (data.access) {
                    console.log("JWT Token:", data.access);
                    localStorage.setItem('jwt', data.access);
                    alert('Login successful!');
                } else {
                    console.error("Login failed:", data);
                    alert('Login failed!');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('An error occurred during login');
            });
        }

        window.onload = function() {
            console.log("Page loaded. Google Sign-In should initialize soon.");
        };
    </script>
</body>
</html>

尝试了一切,但没有继续前进 使用 javascript 更具体地说 HTML 页面从 google 获取 idToken,然后将其发送到后端进行验证和其他 jwt 生成

javascript django oauth-2.0
1个回答
0
投票

请在此输入您的 ID

<meta name="google-signin-client_id" content="my client id">

content="我的客户 ID"

您的控制台中也出现找不到图标的错误。所以正确加载你的网站图标

© www.soinside.com 2019 - 2024. All rights reserved.