-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess_drive.py
More file actions
56 lines (46 loc) · 1.94 KB
/
access_drive.py
File metadata and controls
56 lines (46 loc) · 1.94 KB
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
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Path to your downloaded JSON credentials file
# Scope sets the correct API endpoint to access.
creds = service_account.Credentials.from_service_account_file(
'secrets/service-account.json',
#scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']
scopes=['https://www.googleapis.com/auth/drive.readonly']
)
# Initialize the Drive API client
service = build('drive', 'v3', credentials=creds)
# Function to list files in Google Drive
def list_drive_files():
results = service.files().list(pageSize=10, fields="files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print(f"{item['name']} ({item['id']})")
# Call the function to list files
list_drive_files()
# Path to your downloaded JSON credentials file
# Scope sets the correct API endpoint to access.
creds = service_account.Credentials.from_service_account_file(
'secrets/service-account.json',
scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']
#scopes=['https://www.googleapis.com/auth/drive.readonly']
)
# Access content
sheets_service = build('sheets', 'v4', credentials=creds)
# Function to get columns from a Google Sheets file
def get_sheet_columns(spreadsheet_id, range_name='Weininventur!A1:Z1'):
# Access the data in the specified range
sheet = sheets_service.spreadsheets()
result = sheet.values().get(spreadsheetId=spreadsheet_id, range=range_name).execute()
header_row = result.get('values', [])
# Display the columns (header row)
if header_row:
print("Columns:", header_row[0]) # Display the first row as column names
else:
print("No data found.")
# Replace with your actual spreadsheet ID
spreadsheet_id = '1C00vBQCW0nYRX0tRtVWf-Wyq-9GmmM8HmuC5jzO2QX0'
get_sheet_columns(spreadsheet_id)