can't get user profile info using Oauth2 in server using id_token provided by client

huangapple go评论135阅读模式
英文:

can't get user profile info using Oauth2 in server using id_token provided by client

问题

我尝试使用Google ID在我的网页上登录。我在控制台中记录了用户的id_token。然后我将其复制并传递给服务器,尝试获取用户信息。但是在Golang服务器中出现错误:

> 错误是oauth2: 无法获取令牌:400 Bad Request
响应:{
"error" : "invalid_grant"
}

这是我的服务器端代码。

  1. func main() {
  2. go func() {
  3. http.ListenAndServe(":8123", nil)
  4. }()
  5. http.HandleFunc("/", serveFile)
  6. http.HandleFunc("/loginUser", loginUser)
  7. <-quit
  8. }
  9. func loginUser(rw http.ResponseWriter, req *http.Request) {
  10. id_token, _ := getIdToken(req)
  11. conf := oauth2.Config{
  12. ClientID: "HIDDEN.apps.googleusercontent.com",
  13. ClientSecret: "HIDDEN",
  14. Scopes: []string{
  15. "https://www.googleapis.com/auth/userinfo.email",
  16. "https://www.googleapis.com/auth/userinfo.profile",
  17. },
  18. Endpoint: google.Endpoint,
  19. }
  20. L.Errorln(id_token)
  21. tok, err := conf.Exchange(oauth2.NoContext, id_token)
  22. if err != nil {
  23. L.Errorln("err is", err)
  24. }
  25. L.Errorln("token is ", tok)
  26. response, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + tok.AccessToken)
  27. defer response.Body.Close()
  28. contents, err := ioutil.ReadAll(response.Body)
  29. L.Errorln(contents, err)
  30. }

这是我的客户端代码。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="google-signin-scope" content="profile email">
  5. <meta name="google-signin-client_id" content="HIDDEN.apps.googleusercontent.com">
  6. <script src="https://apis.google.com/js/client:platform.js?" async defer></script>
  7. <script src="/login.js"></script>
  8. <link rel="stylesheet" type="text/css" href="/login.css">
  9. <title>Wander</title>
  10. </head>
  11. <body>
  12. <div id="g-login" class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
  13. <a href="#" onclick="signOut();">Sign out</a>
  14. <script>
  15. function signOut() {
  16. var auth2 = gapi.auth2.getAuthInstance();
  17. auth2.signOut().then(function () {
  18. console.log('User signed out.');
  19. });
  20. }
  21. </script>
  22. </body>
  23. </html>

login.js

  1. function onSignIn(googleUser) {
  2. // Useful data for your client-side scripts:
  3. var profile = googleUser.getBasicProfile();
  4. console.log("ID: " + profile.getId()); // 不要直接发送给服务器!
  5. console.log('Full Name: ' + profile.getName());
  6. console.log('Given Name: ' + profile.getGivenName());
  7. console.log('Family Name: ' + profile.getFamilyName());
  8. console.log("Image URL: " + profile.getImageUrl());
  9. console.log("Email: " + profile.getEmail());
  10. // 需要传递给后端的ID令牌:
  11. var id_token = googleUser.getAuthResponse().id_token;
  12. console.log("ID Token: " + id_token);
  13. };

希望这些信息对你有帮助!

英文:

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.

  1. func main() {
  2. go func() {
  3. http.ListenAndServe(&quot;:8123&quot;, nil)
  4. }()
  5. http.HandleFunc(&quot;/&quot;, serveFile)
  6. http.HandleFunc(&quot;/loginUser&quot;, loginUser)
  7. &lt;-quit
  8. }
  9. func loginUser(rw http.ResponseWriter, req *http.Request) {
  10. id_token, _ := getIdToken(req)
  11. conf := oauth2.Config{
  12. ClientID: &quot;HIDDEN.apps.googleusercontent.com&quot;,
  13. ClientSecret: &quot;HIDDEN&quot;,
  14. Scopes: []string{
  15. &quot;https://www.googleapis.com/auth/userinfo.email&quot;,
  16. &quot;https://www.googleapis.com/auth/userinfo.profile&quot;,
  17. },
  18. Endpoint: google.Endpoint,
  19. }
  20. L.Errorln(id_token)
  21. tok, err := conf.Exchange(oauth2.NoContext, id_token)
  22. if err != nil {
  23. L.Errorln(&quot;err is&quot;, err)
  24. }
  25. L.Errorln(&quot;token is &quot;, tok)
  26. response, err := http.Get(&quot;https://www.googleapis.com/oauth2/v2/userinfo?access_token=&quot; + tok.AccessToken)
  27. defer response.Body.Close()
  28. contents, err := ioutil.ReadAll(response.Body)
  29. L.Errorln(contents, err)

My client side code as follows

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html&gt;
  3. &lt;head&gt;
  4. &lt;meta name=&quot;google-signin-scope&quot; content=&quot;profile email&quot;&gt;
  5. &lt;meta name=&quot;google-signin-client_id&quot; content=&quot;HIDDEN.apps.googleusercontent.com&quot;&gt;
  6. &lt;script src=&quot;https://apis.google.com/js/client:platform.js?&quot; async defer&gt;
  7. &lt;/script&gt;
  8. &lt;script src=&quot;/login.js&quot;&gt;&lt;/script&gt;
  9. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/login.css&quot;&gt;
  10. &lt;title&gt;Wander&lt;/title&gt;
  11. &lt;/head&gt;
  12. &lt;body&gt;
  13. &lt;div id=&quot;g-login&quot; class=&quot;g-signin2&quot; data-onsuccess=&quot;onSignIn&quot; data-
  14. theme=&quot;dark&quot; &gt;&lt;/div&gt;
  15. &lt;a href=&quot;#&quot; onclick=&quot;signOut();&quot;&gt;Sign out&lt;/a&gt;
  16. &lt;script&gt;
  17. function signOut() {
  18. var auth2 = gapi.auth2.getAuthInstance();
  19. auth2.signOut().then(function () {
  20. console.log(&#39;User signed out.&#39;);
  21. });
  22. }
  23. &lt;/script&gt;
  24. &lt;/body&gt;
  25. &lt;/html&gt;

login.js

  1. function onSignIn(googleUser) {
  2. // Useful data for your client-side scripts:
  3. var profile = googleUser.getBasicProfile();
  4. console.log(&quot;ID: &quot; + profile.getId()); // Don&#39;t send this directly to your server!
  5. console.log(&#39;Full Name: &#39; + profile.getName());
  6. console.log(&#39;Given Name: &#39; + profile.getGivenName());
  7. console.log(&#39;Family Name: &#39; + profile.getFamilyName());
  8. console.log(&quot;Image URL: &quot; + profile.getImageUrl());
  9. console.log(&quot;Email: &quot; + profile.getEmail());
  10. // The ID token you need to pass to your backend:
  11. var id_token = googleUser.getAuthResponse().id_token;
  12. console.log(&quot;ID Token: &quot; + id_token);
  13. };

答案1

得分: 1

你收到的id_token已经包含了你所需的用户信息。

请访问https://jwt.io查找一个Go库来解码你的令牌。

英文:

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.

huangapple
  • 本文由 发表于 2017年4月30日 20:16:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/43706248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定