-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_api_auth.py
42 lines (38 loc) · 1.76 KB
/
google_api_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import googleapiclient.errors
from googleapiclient.discovery import build #pip install google-api-python-client
from google_auth_oauthlib.flow import InstalledAppFlow #pip install google-auth-oauthlib
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
import os
TOKEN_NAME = "token.json" # Don't change
# Setup Google
SCOPES = ["https://www.googleapis.com/auth/youtube.upload"]
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
client_secrets_file = "googleAPI.json"
def google_auth():
# Handle GoogleAPI oauthStuff
print("[ GoogleAuth ] - Start authenticating to Google API")
credentials = None
# The file token1.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(TOKEN_NAME):
credentials = Credentials.from_authorized_user_file(TOKEN_NAME, SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
client_secrets_file, SCOPES)
credentials = flow.run_console()
# Save the credentials for the next run
with open(TOKEN_NAME, 'w') as token:
token.write(credentials.to_json())
try:
youtube = build('youtube', 'v3', credentials=credentials)
print("[ GoogleAuth ] - Authentication successful")
return youtube
except googleapiclient.errors.HttpError as e:
print("[ GoogleAuth ] - Authentication failed with error:" + str(e))
return False