首页 > 解决方案 > 有没有办法获取 profile.getEmail() 返回值以从 html 表单填充输入字段?

问题描述

所以基本上我想让 profile.getEmail() 进入电子邮件表单输入或进入 em。对不起,如果这个问题很愚蠢,但我已经在这件事上浪费了几天时间。使用表单输入字段中的数据,我将在 golang 服务器上进行插入查询。所以实际上我需要来自谷歌账户的数据来注册和/或登录服务器。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Index</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="google-signin-scope" content="profile email">
    <meta name="google-signin-client_id" content="MYCLIENTID">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    
</head>
<body>
    <h1>Login</h1> 
    <form action ="/login_process" method="POST">
        <label for="email">Email</label>
        <input type = "text" name="email" id="email" placeholder = "email address"></br></br>
        <label for="password">Password</label>
        <input type= "password" id="password" name="password"> </br></br>
        <input type = "submit">
    </form>
       <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
    <script>
      function onSignIn(googleUser) {
        // Useful data for your client-side scripts:
        var profile = googleUser.getBasicProfile();
        console.log("ID: " + profile.getId()); // Don't send this directly to your server!
        console.log('Full Name: ' + profile.getName());
        console.log('Given Name: ' + profile.getGivenName());
        console.log('Family Name: ' + profile.getFamilyName());
        console.log("Image URL: " + profile.getImageUrl());
        console.log("Email: " + profile.getEmail());
        

        // The ID token you need to pass to your backend:
        var id_token = googleUser.getAuthResponse().id_token;
        console.log("ID Token: " + id_token);
      }
    </script>
    </br></br></br>
    <h1>Signup New User</h1>
    <form action ="/signup" method="POST">
        <label for="signup_email">Email</label>
        <input type = "text" name="signup_email" id="signup_email" placeholder = "email address"></br></br>
        <label for="signup_password">Password</label>
        <input type= "password" id="signup_password" name="signup_password"></br></br>
        <label for="signup_password_confirm">Confirm password</label>
        <input type= "password" id="signup_password_confirm" name="signup_password_confirm"> </br></br>
        <label for="signup_age">Age</label>
        <input type = "text" id="signup_age" name ="signup_age" placeholder = "how old are you?"></br></br>
        <label for="signup_gender">Gender</label>
        <input type = "text" id="signup_gender" name ="signup_gender"></br></br>
        <input type = "submit">
    </form>
</body>
</html>

标签: htmloauth-2.0

解决方案


解决了,document.getElementById("mytext").value = "My value";


推荐阅读