gogoogle-apigoogle-oauthgmail-apigoogle-api-go-client

Go gmail api quickstart results in localhost refused to connect ERR_CONNECTION_REFUSED


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

site can't be reached image

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.


Solution

  • 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.