go-samples
go-samples copied to clipboard
gmail quickstart.go will not retrieve a token
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
- Follow the quickstart.go guide
- When the instructions ask to go to http://localhost:8000 you will get the can't be reached dialog box
Specifications
- Go version (
go version
) go version go1.19.1 linux/amd64 - OS (Mac/Linux/Windows) Linux POP!_OS 22.04 LTS / 64-bit
I am seeing the same problem.
same here
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.
This same issue happens with the Google Drive quickstart.go
I have the same problem
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.
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 https://github.com/googleworkspace/go-samples/issues/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!
Did anyone have a better example?
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])
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 thecode={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.