To Install Google Python Client Library
pip install –upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
google_apis.py Source Code
Last Updated: 01/19/2023
import os
import datetime
from collections import namedtuple
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
def create_service(client_secret_file, api_name, api_version, *scopes, prefix=''):
CLIENT_SECRET_FILE = client_secret_file
API_SERVICE_NAME = api_name
API_VERSION = api_version
SCOPES = [scope for scope in scopes[0]]
creds = None
working_dir = os.getcwd()
token_dir = 'token files'
token_file = f'token_{API_SERVICE_NAME}_{API_VERSION}{prefix}.json'
### Check if token dir exists first, if not, create the folder
if not os.path.exists(os.path.join(working_dir, token_dir)):
os.mkdir(os.path.join(working_dir, token_dir))
if os.path.exists(os.path.join(working_dir, token_dir, token_file)):
creds = Credentials.from_authorized_user_file(os.path.join(working_dir, token_dir, token_file), SCOPES)
# with open(os.path.join(working_dir, token_dir, token_file), 'rb') as token:
# cred = pickle.load(token)
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(CLIENT_SECRET_FILE, SCOPES)
creds = flow.run_local_server(port=0)
with open(os.path.join(working_dir, token_dir, token_file), 'w') as token:
token.write(creds.to_json())
try:
service = build(API_SERVICE_NAME, API_VERSION, credentials=creds, static_discovery=False)
print(API_SERVICE_NAME, API_VERSION, 'service created successfully')
return service
except Exception as e:
print(e)
print(f'Failed to create service instance for {API_SERVICE_NAME}')
os.remove(os.path.join(working_dir, token_dir, token_file))
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
Google.py Source Code

so helpful, thank you
Yes, it’s so good, thanks!
Hi I want to download your google.py sourscode from https://learndataanalysis.org/google-py-file-source-code/.
But when I copy the text it gives me this result:
const pluckDeep = key => obj => key.split(‘.’).reduce((accum, key) => accum[key], obj)
const compose = (…fns) => res => fns.reduce((accum, next) => next(accum), res)
const unfold = (f, seed) => {
const go = (f, seed, acc) => {
const res = f(seed)
return res ? go(f, res[1], acc.concat([res[0]])) : acc
}
return go(f, seed, [])
}
Which I am guessing is not the correct module to import the Create_Service from Google
Hi!
Thanks for all the above and the youtube tutorials.
Unfortunately, as soon as I set up the first few variables and run Create_Service, I get the “Failed to create service instance for…” message on console, despite being taken to the autentification flow and giving consent.
I can’t get around the EXCEPT rule in the modules above.
Ironically, if I run the quickstart tutorial, the token file gets created.
Any help?
Firstly create a folder specific to this API and work.
Then just copy the Google.py file to the folder.
then make a file named “testing.py” and copy the code which was in the youtube vide0 : https://www.youtube.com/watch?v=1JkKtGFnua8
Then run the testing.py file .
Then it will work nicely and the code will return the output .
Hi
I am a newbie in python and I’m trying to implement this on my local and I got these errors. My script is on “SheetJupyter_demo.py”. The error occurs when I try to import the Create_Service on line 3. Which in turn takes me to Google.py line 11. No module named ‘googleapiclient’
My question is how can I get the googleapiclient? Because I think these two errors are related.
Traceback (most recent call last):
File “path\SheetJupyter_demo.py”, line 3, in
from Google import Create_Service
File “path\Google.py”, line 11, in
from googleapiclient.discovery import build
ModuleNotFoundError: No module named ‘googleapiclient’
Any to help?
I think you need to install the module, I think its “pip install google-api-python-client”
‘SCOPES = [scope for scope in scopes[0]]’ should not include the [0]. that will make this pull apart the first entry in scope letter by letter when creating the authorization.
The script has been working for me and many people does the past few years, not sure what you are talking about.
ExcelWriter::save is deprecated. Change to “close()” instead.