dongrenzheng1619 2017-04-11 07:16
浏览 244
已采纳

从访问令牌获取Google登录个人资料信息-Golang

I have the following JavaScript code

auth2.grantOfflineAccess().then(function(codeData) {
    if (!codeData) {
        alert("Something went wrong");
        return;
    }

    $.post("/do/signIn/google", codeData, function() { ... });
});

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条回答 默认 最新

  • douqi1928 2017-04-11 11:02
    关注

    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.

    func decodeGoogleIDToken(idToken string) (gplusID string, err error) {
        var set struct {
            Sub string
        }
        if idToken != "" {
            // Check that the padding is correct for a base64decode
            parts := strings.Split(idToken, ".")
            if len(parts) < 2 {
                return "", fmt.Errorf("Malformed ID token")
            }
            // Decode the ID token
            s := parts[1]
            switch len(s) % 4 {
            case 2:
                s += "=="
            case 3:
                s += "="
            }
    
            b, err := base64.URLEncoding.DecodeString(s)
            if err != nil {
                return "", fmt.Errorf("Malformed ID token: %v", err)
            }
            err = json.Unmarshal(b, &set)
            if err != nil {
                return "", fmt.Errorf("Malformed ID token: %v", err)
            }
        }
        return set.Sub, nil
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题