doucai7294 2016-04-17 01:27
浏览 494
已采纳

使用软件包“ golang.org/x/oauth2”对oauth2进行身份验证

I am trying to figure out how I can use the package golang.org/x/oauth2 to authenticate on a site supporting oauth2.

The code I wrote below works, I am just curious if this is the right approach, using this particular library, to obtain an *http.Client:

func handleCallback(w http.ResponseWriter, r *http.Request) {
    state := r.FormValue("state")
    if state != oauthStateString {
        fmt.Printf("invalid oauth state, expected '%s', got '%s'
", oauthStateString, state)
        http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
        return
    }

    code := r.FormValue("code")
    token, err := oauthConf.Exchange(oauth2.NoContext, code)
    if err != nil {
        fmt.Printf("oauthConf.Exchange() failed with '%s'
", err)
        http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
        return
    }

    ts := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: token.AccessToken},
    )

    tc := oauth2.NewClient(oauth2.NoContext, ts) // got *http.Client
  • 写回答

1条回答 默认 最新

  • douyun1852 2016-12-24 20:26
    关注

    Follow up: I've used it and tweaked it several times and it does the job.

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料