Http 使用GO中的#解析url
所以我收到一个对我的服务器的请求,看起来有点像这样Http 使用GO中的#解析url,http,go,request,Http,Go,Request,所以我收到一个对我的服务器的请求,看起来有点像这样 http://localhost:8080/#access_token=tokenhere&scope=scopeshere 我似乎找不到从url解析令牌的方法 如果那是一个?我可以用一个标准的查询参数来解析它 我试图在/甚至完整的URL之后获取所有内容,但没有成功 非常感谢您的帮助 编辑: 所以我现在已经解决了这个问题,正确的答案是你不能在围棋中真正做到这一点。因此,我制作了一个简单的包,它将在浏览器端执行此操作,然后将令牌发送回服
http://localhost:8080/#access_token=tokenhere&scope=scopeshere
我似乎找不到从url解析令牌的方法
如果那是一个?我可以用一个标准的查询参数来解析它
我试图在/甚至完整的URL之后获取所有内容,但没有成功
非常感谢您的帮助
编辑:
所以我现在已经解决了这个问题,正确的答案是你不能在围棋中真正做到这一点。因此,我制作了一个简单的包,它将在浏览器端执行此操作,然后将令牌发送回服务器
如果您试图在GO中使用本地twitch API,请查看:
客户端甚至(通常)不会将锚定部分发送到服务器
例如,浏览器不发送它。对于解析url,请使用golangnet/url包: OBS:您应该使用授权头来发送身份验证令牌 从示例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
看起来您正在使用OAuth2系统。也许你应该使用授权码授权,而不是隐式授权。@TimCooper也许我应该更清楚一点,应用程序实际上是在用户计算机上本地运行的,只是打开一个http服务器来捕获带有令牌的重定向。“因此,我收到一个对我的服务器的请求,看起来有点像这样
http://localhost:8080/#access_token
“不,你不是。片段不会被发送,因此也不会被接收。请修复客户端,使其在
之后发送?
之后。这不是真正的服务器端问题,只是一个坏客户端未能正确使用HTTP。感谢您的回复,但不幸的是(至少据我所知),我实际上无法从服务器请求访问完整的URL。我有一个函数,在对“/”的任何请求中都会被调用,但调用该函数时,URL片段似乎完全为空。@SimplySerenity,这是意料之中的。URL的锚定部分仅限于客户端,不应发送到服务器。对于桌面应用程序,这有助于我使用a打开登录URL,然后解析重定向中的片段。非常感谢。