go - 无法使用客户端提供的 id_token 在服务器中使用 Oauth2 获取用户个人资料信息

标签 go oauth-2.0 google-identity

我试图在我的网页上使用 google id 登录。我在控制台中记录了用户的 id_token。然后我复制它并传递给服务器并尝试获取用户信息。但是我在 golang 服务器中收到一个错误

err is oauth2: cannot fetch token: 400 Bad Request Response: { "error" : "invalid_grant" }

这是我的服务端代码。

func main() {
go func() {
    http.ListenAndServe(":8123", nil)
}()
http.HandleFunc("/", serveFile)
http.HandleFunc("/loginUser", loginUser)
<-quit
}
func loginUser(rw http.ResponseWriter, req *http.Request) {
id_token, _ := getIdToken(req)
conf := oauth2.Config{
    ClientID:     "HIDDEN.apps.googleusercontent.com",
    ClientSecret: "HIDDEN",
    Scopes: []string{
        "https://www.googleapis.com/auth/userinfo.email",
        "https://www.googleapis.com/auth/userinfo.profile",
    },
    Endpoint: google.Endpoint,
}
L.Errorln(id_token)
tok, err := conf.Exchange(oauth2.NoContext, id_token)
if err != nil {
    L.Errorln("err is", err)
}

L.Errorln("token is ", tok)
response, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + tok.AccessToken)

defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
L.Errorln(contents, err)

我的客户端代码如下

<!DOCTYPE html>
<html>
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="HIDDEN.apps.googleusercontent.com">
 <script src="https://apis.google.com/js/client:platform.js?" async defer>
 </script>
 <script  src="/login.js"></script>
 <link rel="stylesheet" type="text/css" href="/login.css">
 <title>Wander</title>
 </head>
 <body>
 <div id="g-login" class="g-signin2" data-onsuccess="onSignIn" data-
 theme="dark" ></div>

<a href="#" onclick="signOut();">Sign out</a>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
  console.log('User signed out.');
});
}
</script>
</body>
</html>

登录.js

    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);
};

最佳答案

您收到的id_token已经包含了您需要的用户信息。

参见 https://jwt.io找到一个 Go 库来解码你的 token 。

关于go - 无法使用客户端提供的 id_token 在服务器中使用 Oauth2 获取用户个人资料信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43706248/

相关文章:

go - 如何以新的方式构建 Golang 模块和项目结构

go - image.Decode 在解码 .png 文件时返回错误

google-identity - 谷歌YOLO停止工作: The client origin is not permitted to use this API

flutter - GoogleAuthProvider - 选择帐户的选项

laravel - 如何在 Laravel Passport 中获取刷新 token ?

google-identity - 使用 Google 自动(新)登录/保持登录状态 - 网络应用

去吧, golang : traverse through struct

unit-testing - 如何对pubsub进行单元测试接收回调

python - 如何在不使用 gdata oauth2 工作流程的情况下授权 gdata 客户端?

javascript - 在网络应用程序中使用 google-sign.in : sign out using Sign-In button