Spaces:
Runtime error
Runtime error
simplified with sa
Browse fileshttps://youtu.be/4ssigWmExak
https://developers.google.com/identity/protocols/oauth2/service-account#python
- quickstart.py +20 -56
quickstart.py
CHANGED
|
@@ -1,63 +1,27 @@
|
|
| 1 |
-
from __future__ import print_function
|
| 2 |
-
|
| 3 |
-
import os.path
|
| 4 |
-
|
| 5 |
-
from google.auth.transport.requests import Request
|
| 6 |
-
from google.oauth2.credentials import Credentials
|
| 7 |
-
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 8 |
from googleapiclient.discovery import build
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
# If modifying these scopes, delete the file token.json.
|
| 12 |
-
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
|
| 13 |
-
|
| 14 |
-
# The ID and range of a sample spreadsheet.
|
| 15 |
-
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
|
| 16 |
-
SAMPLE_RANGE_NAME = 'Class Data!A2:E'
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def main():
|
| 20 |
-
"""Shows basic usage of the Sheets API.
|
| 21 |
-
Prints values from a sample spreadsheet.
|
| 22 |
-
"""
|
| 23 |
-
creds = None
|
| 24 |
-
# The file token.json stores the user's access and refresh tokens, and is
|
| 25 |
-
# created automatically when the authorization flow completes for the first
|
| 26 |
-
# time.
|
| 27 |
-
if os.path.exists('token.json'):
|
| 28 |
-
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
|
| 29 |
-
# If there are no (valid) credentials available, let the user log in.
|
| 30 |
-
if not creds or not creds.valid:
|
| 31 |
-
if creds and creds.expired and creds.refresh_token:
|
| 32 |
-
creds.refresh(Request())
|
| 33 |
-
else:
|
| 34 |
-
flow = InstalledAppFlow.from_client_secrets_file(
|
| 35 |
-
'credentials.json', SCOPES)
|
| 36 |
-
creds = flow.run_local_server(port=0)
|
| 37 |
-
# Save the credentials for the next run
|
| 38 |
-
with open('token.json', 'w') as token:
|
| 39 |
-
token.write(creds.to_json())
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
|
| 47 |
-
range=SAMPLE_RANGE_NAME).execute()
|
| 48 |
-
values = result.get('values', [])
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
return
|
| 53 |
|
| 54 |
-
|
| 55 |
-
for row in values:
|
| 56 |
-
# Print columns A and E, which correspond to indices 0 and 4.
|
| 57 |
-
print('%s, %s' % (row[0], row[4]))
|
| 58 |
-
except HttpError as err:
|
| 59 |
-
print(err)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from googleapiclient.discovery import build
|
| 2 |
+
from google.oauth2 import service_account
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
SERVICE_ACCOUNT_FILE = 'keys.json'
|
| 5 |
+
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
|
| 6 |
|
| 7 |
+
creds = service_account.Credentials.from_service_account_file(
|
| 8 |
+
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# The ID of a sample spreadsheet.
|
| 11 |
+
SPREADSHEET_ID = '1DNoNf4glcuMxKoVzHVrFo-MktmsVji1wf4IHeraWH84'
|
|
|
|
| 12 |
|
| 13 |
+
service = build('sheets', 'v4', credentials=creds)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Call the Sheets API
|
| 16 |
+
sheet = service.spreadsheets()
|
| 17 |
+
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
|
| 18 |
+
range="A1:B2").execute()
|
| 19 |
+
values = result.get('values', [])
|
| 20 |
+
print(values)
|
| 21 |
|
| 22 |
+
aoa = [["3/1/2022", 4000],["4/4/2022", 3000],["7/12/2022", 7000]]
|
| 23 |
+
request = sheet.values().update(spreadsheetId=SPREADSHEET_ID,
|
| 24 |
+
range="Sheet2!B2", valueInputOption="USER_ENTERED",
|
| 25 |
+
body={"values":aoa})
|
| 26 |
+
response = request.execute()
|
| 27 |
+
print(response)
|