在url中获取片段值

时间:2018-02-05 13:07:14

标签: go httphandler

示例代码



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="tabs-nav col">
    <nav>
      <a>Tab #1 is very long, maybe too long, no it's not. Just to see the triangle responsive height</a>
      <a>Tab #2</a>
      <a>Tab #3</a>
    </nav>
  </div>
  <div class="tabs tabs-content col">
    <div class="content">
      <p>Content #1</p>
      <p>Content #1</p>
      <p>Content #1</p>
      <p>Content #1</p>
      <p>Content #1</p>
      <p>Content #1</p>
      <p>Content #1</p>
      <p>Content #1</p>
      <p>Content #1</p>
    </div>
    <div class="content">
      <p>Content #2</p>
    </div>
    <div class="content">
      <p>Content #3</p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

我尝试使用上面的代码从URL获取片段但是没有成功。

使用的示例网址为: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") }

现在,对于这样的URL,我想在http://localhost:8080/handle#access_token=*1234$111&token_type=bearer&expires_in=3600&scope=onedrive.readwrite&user_id=hashed函数中获取片段access_token的值,该函数基本上是一个http处理程序。

2 个答案:

答案 0 :(得分:1)

正如评论者已指出的那样,片段不是您请求的一部分。片段可以是HTML标记的一个子集,由某个ID标识 - 这是服务器返回的内容。

我想知道你为什么不使用GET参数?

然后您的网址可能是

http://localhost:8080/handle?access_token=*1234$111&token_type=bearer&expires_in=3600&scope=onedrive.readwrite&user_id=hashed

?

之后的#代替/handle

然后您将正确看到access_token GET参数:

inside handle function, 
1 <nil>
2 /handle?access_token=*1234$111&token_type=bearer&expires_in=3600&scope=onedrive.readwrite&user_id=hashed
3 
4 *1234$111

希望这有帮助。

答案 1 :(得分:1)

来自https://github.com/OneDrive/onedrive-api-docs/issues/9

的无耻副本
  

OneDrive使用Microsoft帐户作为其身份提供商   实现OAuth 2.0协议。根据OAuth 2.0规范,访问权限   令牌在重定向uri的片段组件中返回   响应类型设置为&#34; token&#34;。你可以在这里阅读更多相关信息   https://tools.ietf.org/html/rfc6749#section-4.2.2

     

如果您想在查询参数中使用访问令牌,请考虑   使用&#34;代码&#34;流动。

在这里,您可以找到有关此代码流的信息&#39;: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/msa-oauth