使用默认浏览器打开带参数的 URL。

huangapple go评论89阅读模式
英文:

open url with parameters in default browser

问题

如果我们尝试从Go打开给定的URL,就像下面的代码所示(适用于Mac):

exec.Command("open", "https://google.com").Run()

但是如果URL带有参数,我们该如何做呢?

例如,如果我们尝试打开一个Facebook登录URL:https://www.facebook.com/v11.0/dialog/oauth?client_id=123456&redirect_uri=https://example/com,该命令将以退出状态1失败。

即使我们在特殊字符?&=之前包含反斜杠,URL也会变成https://www.facebook.com/v11.0/dialog/oauth\?client_id\=123456\&redirect_uri\=https://example/com

如果我们在终端中使用以下命令:

open https://www.facebook.com/v11.0/dialog/oauth\?client_id\=123456\&redirect_uri\=https://example/com

上述URL将在默认浏览器中打开。但是在Go中这样做会失败!

是否有一种方法可以在Go中实现相同的效果?

是的,可以使用exec.Command("open", "-a", "safari", url)来实现。但这不会打开默认浏览器,而用户很可能已经在默认浏览器中登录了。

英文:

If we try to open a given URL from Go, it is quite easy as shown in the code below (for mac):

exec.Command("open", "https://google.com").Run()

But how do we do the same if the URL has parameters?

For example, if we try to open a facebook login URL: "https://www.facebook.com/v11.0/dialog/oauth?client_id=123456&redirect_uri=https://example/com", the command fails with exit status 1.

This is true even when we include backslashes for the special characters ?, & and =. The URL changes to https://www.facebook.com/v11.0/dialog/oauth\?client_id\=123456\&redirect_uri\=https://example/com.

The above URL opens in default browser if we use

open https://www.facebook.com/v11.0/dialog/oauth\?client_id\=123456\&redirect_uri\=https://example/com

command in terminal. But it fails when doing so from Go!

Is there a way to do the same from Go?

And yes, it can be done using exec.Command("open", "-a", "safari", url). But this would not open the default browser, which is where the user is most likely signed in.

答案1

得分: 1

实际上,它在没有反斜杠的情况下也可以工作。原来在终端中需要使用反斜杠,因为终端在将字符串发送到浏览器之前会内部转义反斜杠。但是在Go中,它只是将原始字符串发送到浏览器。

在Go中使用:

exec.Command("open", "https://www.facebook.com/v11.0/dialog/oauth?client_id=123456&redirect_uri=https://example/com")

从终端命令行中使用:

open https://www.facebook.com/v11.0/dialog/oauth\?client_id\=123456\&redirect_uri\=https://example/com
英文:

It actually works without the backslash. Turns out the backslash is needed in terminal, because the terminal internally escapes the backslash before sending the string to the browser. But in case of go, it just sends the original string to the browser.

In Go use:

exec.Command("open", "https://www.facebook.com/v11.0/dialog/oauth?client_id=123456&redirect_uri=https://example/com")

From the terminal command line:

open https://www.facebook.com/v11.0/dialog/oauth\?client_id\=123456\&redirect_uri\=https://example/com

答案2

得分: 0

尝试在末尾添加反斜杠(\)

exec.Command("open", "https://www.facebook.com/v11.0/dialog/oauth\\?client_id/\\=123456/\\&redirect_uri/\\=https://example/com").Run()
英文:

Try appending backslash(\)

exec.Command("open", "https://www.facebook.com/v11.0/dialog/oauth\\?client_id/\\=123456/\\&redirect_uri/\\=https://example/com").Run()

huangapple
  • 本文由 发表于 2021年9月1日 13:33:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/69008263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定