Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gmail quickstart.go will not retrieve a token #76

Open
tjmcwiz opened this issue Sep 30, 2022 · 11 comments
Open

gmail quickstart.go will not retrieve a token #76

tjmcwiz opened this issue Sep 30, 2022 · 11 comments
Assignees

Comments

@tjmcwiz
Copy link

tjmcwiz commented Sep 30, 2022

Expected Behavior

Following the gmail quickstart.go guide should produce a token.json file

Actual Behavior

The instructions ask to go to http://localhost:8000 which produces a site can't be reached dialog box

BTW - the quickstart.py works as expected. There seems to be something missing from the quickstart.go file if it really does need a webserver for localhost:8000.

Steps to Reproduce the Problem

  1. Follow the quickstart.go guide
  2. When the instructions ask to go to http://localhost:8000 you will get the can't be reached dialog box
    Screenshot from 2022-09-30 10-24-37

Screenshot from 2022-09-30 10-30-03

Specifications

  • Go version (go version) go version go1.19.1 linux/amd64
  • OS (Mac/Linux/Windows) Linux POP!_OS 22.04 LTS / 64-bit
@symbiont-ji
Copy link

I am seeing the same problem.

@wogri
Copy link

wogri commented Oct 3, 2022

same here

@addisonjones3
Copy link

I was having the same problem as well.

A workaround is to configure as stated in the instructions, then when running go run quickstart.go, follow the URL printed to stdout. That will lead you through the consent page and eventually redirect to localhost. That localhost call won't go anywhere, but if you copy the code={copy the value here}&state... from the URL into the terminal window and paste, it will complete the token request flow and the sample will list the labels as intended.

This isn't a long term solution and I'd encourage the Google folks to follow through on a more comprehensive fix.

@razeenf
Copy link

razeenf commented Dec 21, 2022

This same issue happens with the Google Drive quickstart.go

@Frosin
Copy link

Frosin commented Jan 26, 2023

I have the same problem

@biggianteye
Copy link

This same issue happens with the Google Drive quickstart.go

Same problem with the Google Sheets example. Seems like a general problem, which is disappointing given this is intended to be learning material.

@porridge
Copy link

This problem is also present in the calendar example, and I suspect many other too.

The reason is that the getTokenFromWeb function seems to implicilty rely on the out-of-band token transfer functionality which was removed for good in January 2023, as mentioned in #83. New clients seem to default to a http://localhost redirect URL, for which to work seamlessly the program would have to actively be listening on a port.

I happened to stumble upon exactly such listening implementation of this function in another repo. It's almost a drop-in replacement. One just needs to add a few imports, change the function name and pass the context, and it works!

@terrytaochen
Copy link

Did anyone have a better example?

@kafkasl
Copy link

kafkasl commented Apr 28, 2024

hello people, I've found a workaround to get past that issue. I just did the same in python and the localhost redirect doesn't fail so you can complete the browser part. After that running again the golang worked (I actually hit another issue, but at least that seems solved).

Just run:

pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
python download.py`

Here's the code (it's a drive download, haven't cleaned it) download.py

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
import os

# Scopes define the level of access you require.
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']

def download_file(file_name):
    creds = None
    # The file token.json stores the user's access and refresh tokens.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('drive', 'v3', credentials=creds)

    # Call the Drive v3 API to find the file
    results = service.files().list(q=f"name='{file_name}'",
                                   spaces='drive',
                                   fields='nextPageToken, files(id, name)').execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        file_id = items[0]['id']
        request = service.files().get_media(fileId=file_id)
        file_io = open(file_name, 'wb')
        downloader = MediaIoBaseDownload(file_io, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
        file_io.close()
        print(f"Downloaded '{file_name}' successfully.")

if __name__ == '__main__':
    import sys
    if len(sys.argv) != 2:
        print("Usage: python download_from_drive.py 'filename'")
        sys.exit(1)
    download_file(sys.argv[1])

@vishalvivekm
Copy link

I was having the same problem as well.

A workaround is to configure as stated in the instructions, then when running go run quickstart.go, follow the URL printed to stdout. That will lead you through the consent page and eventually redirect to localhost. That localhost call won't go anywhere, but if you copy the code={copy the value here}&state... from the URL into the terminal window and paste, it will complete the token request flow and the sample will list the labels as intended.

This isn't a long term solution and I'd encourage the Google folks to follow through on a more comprehensive fix.

Thanks much, this helped.

@scarrington76
Copy link

thank you addisonjones3 for the solution - worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests