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>
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
As commenters pointed already, fragment is not part of your request. Fragment can be rather a subset of your HTML markup, identified by some ID - that'd be a thing returned by the server.
I am wondering why wouldn't you use GET parameters?
Then your URL could be
http://localhost:8080/handle?access_token=*1234$111&token_type=bearer&expires_in=3600&scope=onedrive.readwrite&user_id=hashed
that is, with ?
instead of #
after the /handle
Then you will see your access_token
GET parameter properly:
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
Hope this helps.