dounan9070 2017-04-30 12:16
浏览 288

使用客户端提供的id_token在服务器中使用Oauth2无法获取用户配置文件信息

I tried to log in using the google id on my web page. I logged the id_token from the user in console. Then I copied that and passed to a server and tried to get user info. But I get an error in golang server as

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

This is my serve side code.

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)

My client side code as follows

<!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>

login.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);
};
  • 写回答

1条回答 默认 最新

  • dongmanni6916 2017-04-30 14:46
    关注

    The id_token you received already contains the information of the user you need.

    See https://jwt.io to find a Go library to decode your token.

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持