微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

golang中的google oauth:oauth2:无法获取令牌:401未经授权的响应:{“错误”:“invalid_client”,“error_description”:“未授权”}

如何解决golang中的google oauth:oauth2:无法获取令牌:401未经授权的响应:{“错误”:“invalid_client”,“error_description”:“未授权”}

我尝试通过谷歌使用 Oauth2.0 该错误发生在我选择登录帐户的那一刻。

func googleAuthCallbackHandler(w http.ResponseWriter,r *http.Request) {
    oauthstate,err := r.Cookie("oauthstate")
    if err != nil {
        log.Printf("oauthstate is not Founded%s",err.Error())
        http.Redirect(w,r,"/",http.StatustemporaryRedirect)
    }
    if r.FormValue("state") != oauthstate.Value {
        log.Printf("invalid google oauth state cookie: %s state:%s\n",oauthstate.Value,r.FormValue("state"))
        http.Redirect(w,http.StatustemporaryRedirect)
    }

    data,err := getGoogleUserInfo(r.FormValue("code"))
    if err != nil {
        log.Printf("Failed to load code value %s",http.StatustemporaryRedirect)
    }
    fmt.Fprint(w,string(data))
}

func getGoogleUserInfo(code string) ([]byte,error) {
    token,err := googleOauthConfig.Exchange(context.Background(),code)
    if err != nil {
        return nil,fmt.Errorf("Failed to Exchange %s\n",err.Error())
    }
    res,err := http.Get(oauthGoogleUrlAPI + token.Accesstoken)
    if err != nil {
        return nil,fmt.Errorf("Failed to Get User Info %s\n",err.Error())
    }

    return IoUtil.ReadAll(res.Body)
}

错误代码

021/06/04 12:19:32 4/0AY0e-g79Vb8Up5fvjuel39sstaf0Hf__Amfm8TO3qbaIKHc2kSk63jB9Mso8JS96rYTS8Q
2021/06/04 12:19:32 Failed to load code value Failed to Exchange oauth2: cannot fetch token: 401 Unauthorized
Response: {
  "error": "invalid_client","error_description": "Unauthorized"
}

我认为是交换功能引起了问题,但我不知道是什么问题。 请帮帮我

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。