dtbiszu7724 2018-02-05 13:07
浏览 40
已采纳

获取URL中的片段值

sample code

func main() {
    fmt.Print("starting box web server...")
    http.HandleFunc("/", landing)
    http.HandleFunc("/handle", handler)
    http.ListenAndServe(connector_port, nil)
}
func landing(w http.ResponseWriter, r *http.Request) {
    fmt.Println("redirecting to login for authentication...")
    http.Redirect(w, r, "http://*****urlfortoken", http.StatusFound)
}
func handler(w http.ResponseWriter, r *http.Request) {
    bodyresnew_folder, _ := ioutil.ReadAll(r.Body)
    fmt.Println("response Body:", string(bodyresnew_folder))
    fmt.Println("1", r.GetBody)
    fmt.Println("2", r.URL.String())
    fmt.Println("3", r.URL.Fragment)
    fmt.Println("4", r.URL.Query().Get("access_token"))
    fmt.Println("inside handle function,", r.Form.Get("access_token"))
    fmt.Println("finished processing all files please close this server manually")
}

I tried the code above to get fragment from an URL but was unsuccessful.

Example URL used is: http://localhost:8080/handle#access_token=*1234$111&token_type=bearer&expires_in=3600&scope=onedrive.readwrite&user_id=hashed

Now, for such URL, I want to get the value of fragment access_token in the handler function which is basically an http handler.

</div>
  • 写回答

2条回答 默认 最新

  • doubu1950 2018-02-06 14:21
    关注

    Shameless copy from: https://github.com/OneDrive/onedrive-api-docs/issues/9

    OneDrive uses Microsoft account as its identity provider, which implements the OAuth 2.0 protocol. Per the OAuth 2.0 spec, the access token is returned in the fragment component of the redirect uri when the response type is set to "token". You can read more about it at https://tools.ietf.org/html/rfc6749#section-4.2.2.

    If you would like the access token in the query parameter, consider using the "code" flow.

    Here you will find information about this 'code flow': https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/msa-oauth

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题