2
votes

I am trying to connect to google drive using Oauth on my local machine. I initially tried to follow the quick start steps outlined here https://developers.google.com/drive/v3/web/quickstart/python but it wouldn't connect. I get prompted with the authorisation screen and click allow but I am then redirected to localhost:8080 with an error response message and the log says:

Your browser has been opened to visit:

https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&response_type=code&client_id=xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com&access_type=offline

If your browser is on a different machine then exit and re-run this application with the command-line parameter

--noauth_local_webserver

I have subsequently tried to simply the code to work through it step by step but I keep encountering the same problem that no access code is returned and I can't progress with authentication.

from httplib2 import Http
from oauth2client import file, client, tools
from oauth2client.client import flow_from_clientsecrets

store = file.Storage('credentials.json')
flow = flow_from_clientsecrets('client_secret.json',                        
       scope='https://www.googleapis.com/auth/drive.metadata.readonly',                       
       redirect_uri='http://example.com/auth_return')

print flow
creds = tools.run_flow(flow, store)
print 'access_token: {}'.format(creds.access_token)

service = build('drive', 'v3', http=creds.authorize(Http()))

Any ideas please?

1
For example, how about using the option of --noauth_local_webserver, when you run "quickstart.py"? It's python quickstart.py --noauth_local_webserver. By this, URL is shown in your console. You can do the authorization by accessing the URL using your browser, and you retrieve the code. Then, you can retrieve refresh token by putting the code to the console. If this was not the solution of your situation, I'm sorry.Tanaike

1 Answers

0
votes

In order to authenticate to google you need to first login to your google account and then approve authorization.

Your script is telling you to navigate to that page and approve the authorization.

https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&response_type=code&client_id=xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxx.apps.googleusercontent.com&access_type=offline

Just open it up on a browser if it hasnt done so for you.

you might consider following the python quickstart

"""
Shows basic usage of the Drive v3 API.

Creates a Drive v3 API service and prints the names and ids of the last 10 files
the user has access to.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))

# Call the Drive v3 API
results = service.files().list(
    pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print('{0} ({1})'.format(item['name'], item['id']))