This is the best way to do a credential check and validation from Google Drive using python. Please feel free to iterate, dwonload and use as you want.
All data in the credetial file is fake so do not forget to update and change.
I've been doing a few Python project just for hobby and one of the projects I need to upload a most recent version of a file gneerated by the python script. I always had major issues with credential validation and refresh of the credentials so instead of having to solve every time an issue happened I tried to build a better version of what was my first iteration of a file transfer scrip to Google Drive.
I do not need to say that you will have to create an account in case you do not have it
- Enable the Gdrive credential in your Google Cloud page: https://console.cloud.google.com/apis/dashboard
- Click on the + ENABLE APIS AND SERVICES link on top below the search bar
- Find the Google Drive API and click on it to enable (should be straight forward but I'll add screenshots later)
- On the left menu or through this direct link https://console.cloud.google.com/apis/credentials you get to where the work needs to be done.
- Click on +CREATE CREDENTIALS on the upper part of the screen below the search bar
- Once you click to create the credentials you need to select OAuth client ID - https://console.cloud.google.com/apis/credentials/oauthclient
- Select Destop App in the dropdown and give a name to your app
- This is essentially it. you now have 0Auth client created
- At this point you can download the JSON file from the link shown on the light box. - Do not share this information or data to anyone.
The file will look something like this:
{
"installed":{
"client_id":"CLIENT-ID",
"project_id":"PROJECT-ID",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"CLIENT-SECRET",
"redirect_uris":[
"http://localhost"
]
}
}
The script use the PyDrive library: https://pypi.org/project/PyDrive/
You should be able to install it by runing pip3 install PyDrive
Once the library is installed here is the script google_authentication.py
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
## Start Google Authentication
gauth = GoogleAuth()
# Load JSON credentials downloaded from Google
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = 'credentials/client_secrets.json'
# Try to use the credential .txt generated at the first 0Auth approval
gauth.LoadCredentialsFile("credentials/mycreds.txt")
# If the app was not yet authenticated it will ask to get the authentication from Google
if gauth.credentials is None:
# Authenticate if they're not there
# gauth.LocalWebserverAuth() # <-- WebApp approval
gauth.CommandLineAuth() # <-- Command Line Approva
# If credential has expired refresh
elif gauth.access_token_expired:
gauth.Refresh()
# Initialize the saved creds
else:
gauth.Authorize()
# Save current credentials into a TXT file for the future
gauth.SaveCredentialsFile("credentials/mycreds.txt")
drive = GoogleDrive(gauth)
After the connection and authentication is done you can run the example script below to list all files
## Example: List all files in the drive
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
More references on PyDrive documentation: https://pythonhosted.org/PyDrive/index.html