所以我收到的服务器请求看起来像这样
http://localhost:8080/#access_token=tokenhere&scope=scopeshere
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到从网址解析令牌的方法.
如果#是一个?我可以解析一个标准的查询参数.
我试图在/甚至是完整的URL之后获取所有内容,但没有运气.
任何帮助是极大的赞赏.
编辑:
所以我现在已经解决了这个问题,正确的答案是你无法在GO中真正做到这一点.所以我创建了一个简单的包,它将在浏览器端执行,然后将令牌发送回服务器.
如果你想在GO中尝试做局部抽搐API,请查看它:
对于解析 url,请使用 golang net/url包: https: //golang.org/pkg/net/url/
OBS:您应该使用 Authorization 标头来发送身份验证令牌。
从示例 url 中提取数据的示例代码:
package main
import (
"fmt"
"net"
"net/url"
)
func main() {
// Your url with hash
s := "http://localhost:8080/#access_token=tokenhere&scope=scopeshere"
// Parse the URL and ensure there are no errors.
u, err := url.Parse(s)
if err != nil {
panic(err)
}
// ---> here is where you will get the url hash #
fmt.Println(u.Fragment)
fragments, _ := url.ParseQuery(u.Fragment)
fmt.Println("Fragments:", fragments)
if fragments["access_token"] != nil {
fmt.Println("Access token:", fragments["access_token"][0])
} else {
fmt.Println("Access token not found")
}
// ---> Others data get from URL:
fmt.Println("\n\nOther data:\n")
// Accessing the scheme is straightforward.
fmt.Println("Scheme:", u.Scheme)
// The `Host` contains both the hostname and the port,
// if present. Use `SplitHostPort` to extract them.
fmt.Println("Host:", u.Host)
host, port, _ := net.SplitHostPort(u.Host)
fmt.Println("Host without port:", host)
fmt.Println("Port:",port)
// To get query params in a string of `k=v` format,
// use `RawQuery`. You can also parse query params
// into a map. The parsed query param maps are from
// strings to slices of strings, so index into `[0]`
// if you only want the first value.
fmt.Println("Raw query:", u.RawQuery)
m, _ := url.ParseQuery(u.RawQuery)
fmt.Println(m)
}
// part of this code was get from: https://gobyexample.com/url-parsing
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2809 次 |
最近记录: |