Google Drive API allows you to create apps that leverage Google Drive cloud storage. You can develop applications that integrate with Google Drive, and create robust functionality in your application using Google Drive API.
Some examples what you can do with Google Drive API?
- Download files from Google Drive and Upload files to Google Drive.
- Search for files and folders stored in Google Drive. Create complex search queries that return any of the file metadata fields in the Files resource.
- Let users share files, folders and drives to collaborate on content.
- Create third-party shortcuts that are external links to data stored outside of Drive, in a different data store or cloud storage system.
- Create a dedicated Drive folder to store your application’s data so that the app cannot access all the user’s content stored in Google Drive. See Store application-specific data
- Manage multiple accounts simultaneously.
- Search files across multiple accounts simultaneously.
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Quota and Scopes
You can make up to 1,000,000,000 (1 billion) Drive API calls per day. If you are exceeding the limit, you can contact Google support to increase the limit.
Authentication
Every request your application sends to the Drive API must include an authorization token. The token also identifies your application to Google.
Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported.
Step 1. Enable Google Drive API service
- Log in to your Google Cloud Platform,
- Select your project
- Enable Google Drive API service
If this is your first time working with Google API service, to learn how to create your first Google Project.
Step 2. Install Python Library
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Google.py
import pickle
import os
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from google.auth.transport.requests import Request
def Create_Service(client_secret_file, api_name, api_version, *scopes):
print(client_secret_file, api_name, api_version, scopes, sep='-')
CLIENT_SECRET_FILE = client_secret_file
API_SERVICE_NAME = api_name
API_VERSION = api_version
SCOPES = [scope for scope in scopes[0]]
print(SCOPES)
cred = None
pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
# print(pickle_file)
if os.path.exists(pickle_file):
with open(pickle_file, 'rb') as token:
cred = pickle.load(token)
if not cred or not cred.valid:
if cred and cred.expired and cred.refresh_token:
cred.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
cred = flow.run_local_server()
with open(pickle_file, 'wb') as token:
pickle.dump(cred, token)
try:
service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
print(API_SERVICE_NAME, 'service created successfully')
return service
except Exception as e:
print('Unable to connect.')
print(e)
return None
def convert_to_RFC_datetime(year=1900, month=1, day=1, hour=0, minute=0):
dt = datetime.datetime(year, month, day, hour, minute, 0).isoformat() + 'Z'
return dt
Hi Jie-Jenn,
I’m a new for google drive API. I finish all the configuration and run the code you shows in this video but I get below error message.
GDAPI_credentials.json-drive-v3-(‘https://www.googleapis.com/auth/drive’,)
[‘h’, ‘t’, ‘t’, ‘p’, ‘s’, ‘:’, ‘/’, ‘/’, ‘w’, ‘w’, ‘w’, ‘.’, ‘g’, ‘o’, ‘o’, ‘g’, ‘l’, ‘e’, ‘a’, ‘p’, ‘i’, ‘s’, ‘.’, ‘c’, ‘o’, ‘m’, ‘/’, ‘a’, ‘u’, ‘t’, ‘h’, ‘/’, ‘d’, ‘r’, ‘i’, ‘v’, ‘e’]
Traceback (most recent call last):
File “C:\Users\Jason\.spyder-py3\temp.py”, line 18, in
service=Create_Service(CLIENT_SERVICE_FILE,API_NAME,API_VERSION,SCOPES)
File “D:\installed_app\anaconda3\lib\site-packages\Google.py”, line 31, in Create_Service
cred = flow.run_local_server()
File “D:\installed_app\anaconda3\lib\site-packages\google_auth_oauthlib\flow.py”, line 457, in run_local_server
local_server = wsgiref.simple_server.make_server(
File “D:\installed_app\anaconda3\lib\wsgiref\simple_server.py”, line 154, in make_server
server = server_class((host, port), handler_class)
File “D:\installed_app\anaconda3\lib\socketserver.py”, line 452, in __init__
self.server_bind()
File “D:\installed_app\anaconda3\lib\wsgiref\simple_server.py”, line 50, in server_bind
HTTPServer.server_bind(self)
File “D:\installed_app\anaconda3\lib\http\server.py”, line 138, in server_bind
socketserver.TCPServer.server_bind(self)
File “D:\installed_app\anaconda3\lib\socketserver.py”, line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted’,))
Do you know how to fix this issue??
Best Regards,
Jason
That happens when you tried to run the script whilst the same script is running in another console/terminal. You can’t use the same port at same time.
Finish the process and try to run again the script.
I followed the steps carefully and it all worked perfectly, but after two months my code completely stopped working and gave the following error:
“google.auth.exceptions.RefreshError: (‘invalid_grant: Bad Request’, {‘error’: ‘invalid_grant’, ‘error_description’: ‘Bad Request’})”
Creating a new google account and credentials somehow leads to the same error, I’m still a beginner at this so I’m not what has gone wrong. Could anyone help me out here?
Thanks
Hey,
I managed to connect and it does work great, btw. you are doing cool stuff and great job sharing all the knowledge you have.
I have a question about a fragment of the code that doesn’t seem to work for me. It’s about stroing the token in .pickle format.
It is not being found I have tried various directiories. The pickle file itself is being stored in Project directory, however it is not accessed by the code from there. Where I should actually store the file in order not to validate my self almost each time i run the code?
Can anyone answer this question ? https://stackoverflow.com/questions/71284767/mismatching-state-csrf-warning-state-not-equal-in-request-and-response-in-dja
I get an error “files” is not a known member of “None” when using the video tutorial “Google Drive API in Python | Download Files” example. When running in VSCode, the error is highlighted but it runs perfectly. When running from the executable the error prevents the program to run. Any ideas?
Facing the same issue. Its looking for files function but its not present in Google.py script.