Part 1 Create Project, OAuth, and Venv
01
Review
jie jenn gmail getting started
and
codeTops gmail getting started.
The instructions are located here under Guides
→ Quickstarts
→ Python
https://developers.google.com/drive/api/quickstart/python
Use tstudystuff gmail for practice
02
Create Project,
03
Enable Drive under Library
04
Follow the instructions from Quickstarts
→ Python
for setting up OAUTH and credentials, review jie jenn and code tops if confused seen in
step 1 at top of this part
add the following scope found at https://developers.google.com/drive/api/guides/api-specific-auth
https://www.googleapis.com/auth/drive
Part 2
01
Put the two scripts in the same directory
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def create_service(client_secret,api_name,api_version,*scopes):
CLIENT_SECRET = client_secret
API_NAME = api_name
API_VERSION = api_version
SCOPES = [scope for scope in scopes[0]]
creds = None
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)
with open ('token.json','w') as token:
token.write(creds.to_json())
try:
service = build(API_NAME,API_VERSION,credentials=creds)
return service
except HttpError as e:
print('error', e)
from googleServiceCreate import create_service
CLIENT_SECRET = 'credentials.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive']
service = create_service(CLIENT_SECRET,API_NAME,API_VERSION,SCOPES)
02
The dir() functions shows the
attributes and methods of the service object, this works for all apis services
from googleServiceCreate import create_service
CLIENT_SECRET = 'credentials.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive']
service = create_service(CLIENT_SECRET,API_NAME,API_VERSION,SCOPES)
print(dir(service))
03
If you go to the reference page https://developers.google.com/drive/api/reference/rest/v3 you can see they are listed here as well.