I have been trying to set up a very simple python program to connect to Google Drive API, I have tried dozens of different approaches I found online but none seem to be working, documentation is all over the place and I cant get it to work.
I am in need of an approach that does not prompt the user to grant access seeing that I am going to be accessing my own personal drive, I would like it to do it automatically without me having to accept every time.
Could anyone send me a complete (very simplistic) working code template that I can use to connect to googles drive API using python?
This is my latest attempt, you might modify this one or create a new one, I just need it to work :(
import google.oauth2.credentials
import google_auth_oauthlib.flow
from oauth2client.client import OAuth2WebServerFlow, FlowExchangeError
# Use the client_secret.json file to identify the application requesting
# authorization. The client ID (from that file) and access scopes are required.
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
'client_secret.json',
scopes=['https://www.googleapis.com/auth/drive.metadata.readonly'])
# Indicate where the API server will redirect the user after the user completes
# the authorization flow. The redirect URI is required.
flow.redirect_uri = 'http://localhost:8888/'
# Generate URL for request to Google's OAuth 2.0 server.
# Use kwargs to set optional request parameters.
authorization_url, state = flow.authorization_url(
# Enable offline access so that you can refresh an access token without
# re-prompting the user for permission. Recommended for web server apps.
access_type='offline',
# Enable incremental authorization. Recommended as a best practice.
include_granted_scopes='true')
print(state)
# code = input('Enter verification code: ').strip()
try:
credentials = flow.step2_exchange(state)
print(json.dumps(json.loads(credentials._to_json([])), sort_keys=True, indent=4))
except FlowExchangeError:
print("Your verification code is incorrect or something else is broken.")
exit(1)
Bonus: I am going to use this to upload a CSV file and then edit the same file with new data
Thanks alot for all the help.