Go gmail api快速入门在本地主机上拒绝连接ERR_CONNECTION_REFUSED。

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

Go gmail api quickstart results in localhost refused to connect ERR_CONNECTION_REFUSED

问题

我正在按照go quickstart gmail api中的步骤进行操作。

getTokenFromWeb函数中,无论是粘贴长网址
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=abcdefg.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly&state=state-token

还是

http://localhost:8000

都会导致以下错误:

> 无法访问此网站。localhost 拒绝连接。ERR_CONNECTION_REFUSED

Go gmail api快速入门在本地主机上拒绝连接ERR_CONNECTION_REFUSED。

按照相同的快速入门指南,但是针对Python的指南可以完美运行。

如果我通过Python获取令牌并在Go快速入门中使用它,也可以正常工作。所以问题只出在从网页检索令牌上。

英文:

I'm following the steps in go quickstart gmail api.

On the function getTokenFromWeb, pasting either the long url
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=abcdefg.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly&state=state-token

or

http://localhost:8000

results in

> This site can't be reached. localhost refused to connect. ERR_CONNECTION_REFUSED

Go gmail api快速入门在本地主机上拒绝连接ERR_CONNECTION_REFUSED。

Following the same quickstart but for python works flawlessly.

If I get the token via python and use it in Go quickstart, it also works. So the issue is just on the token from web retrieval.

答案1

得分: 3

你遇到的问题与oob的移除有关。当该示例最初创建时,oob仍然有效。因此,它会为您显示一个漂亮的网页,您可以在其中复制授权码。

现在这种方法不再有效,所以我们被迫使用http://127.0.0.1或localhost。由于您的计算机显然没有运行Web服务器,因此显示了404错误。

但是,如果您查看URL栏,您将在其中找到您需要的授权码,以便授权您的应用程序。

解决方案很简单,只需从URL栏中复制代码即可。如果您想修复404错误,您需要弄清楚如何启动Web服务器以托管http://127.0.0.1。

Python示例通过运行本地服务器来实现:

creds = flow.run_local_server(port=0)

PHP可以使用类似以下方式实现:

php -S localhost:8000 -t examples/

但是我不确定如何使用Go来实现这一点。

英文:

The issue you are having is related to the removal of oob. When that sample was originally created oob still worked. So it would display a nice web page for you where you could copy the authorization code.

That no longer works so we are forced to use http://127.0.0.1 or localhost. As your machine apparently does not have a web server running its displaying to you a 404 error.

However if you look in the URL bar you will find the authorization code you need in order to authorize your application.

The solution is to simply copy the code from the url bar. If you want to fix the 404 your going to have to figure out how to start a web server in order to host the http://127.0.0.1 from.

The python sample does this by running a local server

creds = flow.run_local_server(port=0)

Php can do it using something like this

php -S localhost:8000 -t examples/

Im not sure how that can be done with Go though.

huangapple
  • 本文由 发表于 2022年11月15日 23:14:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/74448007.html
匿名

发表评论

匿名网友

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

确定