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 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了