-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_sheets.py
More file actions
43 lines (37 loc) · 1.57 KB
/
test_sheets.py
File metadata and controls
43 lines (37 loc) · 1.57 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
import json
from googleapiclient.discovery import build
import socket
import requests
def test_connection():
# Load API key
with open('api_keys.json') as f:
keys = json.load(f)
api_key = keys['GoogleAPIKey']
print("1. Testing basic internet connectivity...")
try:
socket.create_connection(("8.8.8.8", 53), timeout=3)
print("✓ Internet connection is working")
except OSError:
print("✗ No internet connection")
return
print("\n2. Testing Google Sheets API endpoint...")
try:
response = requests.get('https://sheets.googleapis.com/$discovery/rest?version=v4')
print(f"✓ API endpoint is reachable (status code: {response.status_code})")
except Exception as e:
print(f"✗ Cannot reach API endpoint: {e}")
return
print("\n3. Testing API key with Google Sheets...")
try:
service = build('sheets', 'v4',
developerKey=api_key,
discoveryServiceUrl='https://sheets.googleapis.com/$discovery/rest?version=v4')
print("✓ Successfully created Sheets service")
# Try to access the spreadsheet
spreadsheet_id = '1k7VOAgZY9FVdcyVFaQmY_iW_DXvYQluosM2LYL2Wmc8'
result = service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()
print(f"✓ Successfully accessed spreadsheet: {result.get('properties', {}).get('title')}")
except Exception as e:
print(f"✗ Error with API key or spreadsheet access: {e}")
if __name__ == '__main__':
test_connection()