-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
58 lines (47 loc) · 1.69 KB
/
service.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
from google.auth.exceptions import MutualTLSChannelError
# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/spreadsheets.readonly"]
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = "1SvGvOXTO-SMloCklizlcN252uaaOw-9XxEL-x-fDXGw"
SAMPLE_RANGE_NAME = "Sheet1"
def sheet_service():
"""fetch and store from sheets
"""
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=5000)
with open("token.json", "w") as token:
token.write(creds.to_json())
try:
service = build("sheets", "v4", credentials=creds)
return service
# sheet = service.spreadsheets()
# result = (
# sheet.values()
# .get(spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE_NAME)
# .execute()
# )
# values = result.get("values", [])
# if not values:
# print("No data found.")
# return
# print("Name, Major:")
# for row in values:
# # Print columns A and E, which correspond to indices 0 and 4.
# print(f"{row[0]}, {row[-1]}")
except MutualTLSChannelError as err:
print(err)