从访问令牌中获取Google登录配置文件信息 – Golang

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

Get Google Sign In Profile Info From Access Token - Golang

问题

我有以下的JavaScript代码:

  1. auth2.grantOfflineAccess().then(function(codeData) {
  2. if (!codeData) {
  3. alert("出现了一些问题");
  4. return;
  5. }
  6. $.post("/do/signIn/google", codeData, function() { ... });
  7. });

我正在将Google登录的代码发送到我的Golang Web应用程序。
我成功地从POST https://www.googleapis.com/oauth2/v4/token获取了访问令牌。

我的问题是,我无法获取用户的个人资料信息(例如电子邮件、显示名称等)。

我尝试发送一个请求到GET https://www.googleapis.com/auth/userinfo.profile?access_token=xxx,但是我收到了一个空的响应(空白的主体)。

英文:

I have the following JavaScript code

  1. auth2.grantOfflineAccess().then(function(codeData) {
  2. if (!codeData) {
  3. alert("Something went wrong");
  4. return;
  5. }
  6. $.post("/do/signIn/google", codeData, function() { ... });
  7. });

I am sending the code for a Google sign in to my golang web app.
I successfully got an access token from POST https://www.googleapis.com/oauth2/v4/token.

My issue is, I am unable to get the user's profile information (such as email, display name etc.)

I've tried sending a request to GET https://www.googleapis.com/auth/userinfo.profile?access_token=xxx and I receive an empty response (blank body).

答案1

得分: 1

你必须解码通过 POST https://www.googleapis.com/oauth2/v4/token 返回的 Google ID 令牌,将其转换为 Google+ ID,然后可以使用 GET https://www.googleapis.com/plus/v1/people/[gplusID]?access_token=[accessToken]

使用 Go 语言,你可以使用以下函数解码 Google ID 令牌:

  1. func decodeGoogleIDToken(idToken string) (gplusID string, err error) {
  2. var set struct {
  3. Sub string
  4. }
  5. if idToken != "" {
  6. // 检查 base64 解码的填充是否正确
  7. parts := strings.Split(idToken, ".")
  8. if len(parts) < 2 {
  9. return "", fmt.Errorf("Malformed ID token")
  10. }
  11. // 解码 ID 令牌
  12. s := parts[1]
  13. switch len(s) % 4 {
  14. case 2:
  15. s += "=="
  16. case 3:
  17. s += "="
  18. }
  19. b, err := base64.URLEncoding.DecodeString(s)
  20. if err != nil {
  21. return "", fmt.Errorf("Malformed ID token: %v", err)
  22. }
  23. err = json.Unmarshal(b, &set)
  24. if err != nil {
  25. return "", fmt.Errorf("Malformed ID token: %v", err)
  26. }
  27. }
  28. return set.Sub, nil
  29. }

你可以在这里查看一个示例:https://play.golang.org/p/M7sYmE2ztx

英文:

You must decode the Google ID token returned by POST https://www.googleapis.com/oauth2/v4/token to convert it into a Google+ ID then you may use GET https://www.googleapis.com/plus/v1/people/[gplusID]?access_token=[accessToken]

> Using Go, you can decode the google ID token using the following
> function. Normally, it is critical that you validate an ID token
> before you use it, but since you are communicating directly with
> Google over an intermediary-free HTTPS channel and using your Client
> Secret to authenticate yourself to Google, you can be confident that
> the token you receive really comes from Google and is valid. If your
> server passes the ID token to other components of your app, it is
> extremely important that the other components validate the token
> before using it.

Using Go, you can decode the ID token with the following function.

  1. func decodeGoogleIDToken(idToken string) (gplusID string, err error) {
  2. var set struct {
  3. Sub string
  4. }
  5. if idToken != &quot;&quot; {
  6. // Check that the padding is correct for a base64decode
  7. parts := strings.Split(idToken, &quot;.&quot;)
  8. if len(parts) &lt; 2 {
  9. return &quot;&quot;, fmt.Errorf(&quot;Malformed ID token&quot;)
  10. }
  11. // Decode the ID token
  12. s := parts[1]
  13. switch len(s) % 4 {
  14. case 2:
  15. s += &quot;==&quot;
  16. case 3:
  17. s += &quot;=&quot;
  18. }
  19. b, err := base64.URLEncoding.DecodeString(s)
  20. if err != nil {
  21. return &quot;&quot;, fmt.Errorf(&quot;Malformed ID token: %v&quot;, err)
  22. }
  23. err = json.Unmarshal(b, &amp;set)
  24. if err != nil {
  25. return &quot;&quot;, fmt.Errorf(&quot;Malformed ID token: %v&quot;, err)
  26. }
  27. }
  28. return set.Sub, nil
  29. }

Check out an example here at https://play.golang.org/p/M7sYmE2ztx

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

发表评论

匿名网友

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

确定