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 (将#修改为@)