I'm using the following code to download a Gsheet into Python. It works for some sheets but not all.
import pickle
import os.path
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
def gsheet_api_check(SCOPES):
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
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=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return creds
def pull_sheet_data(SCOPES,SPREADSHEET_ID,RANGE_NAME):
creds = gsheet_api_check(SCOPES)
service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()
#result = copy_file(service, SPREADSHEET_ID, copy_title)
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
rows = sheet.values().get(spreadsheetId=SPREADSHEET_ID, range=RANGE_NAME).execute()
data = rows.get('values')
print("COMPLETE: Data copied")
return data
On the sheets that cause an error I recieve the following msg:
HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1bI_X2ssOj9gTYnKsa_CDajYP0FcFMr0E/values/3-7-14days?alt=json returned "This operation is not supported for this document">
I believe the error could be because the original spreadsheet was copied from a .xlsx format. I'm having trouble implementing a copy function to get around this error. Thanks in advance.
UPDATE
I changed the functions so that it incorporate both Drive API and Sheets API
def gsheet_api_check(SCOPES):
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
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=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return creds
def drive_api_check(SCOPES):
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
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('drivecredentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return creds
def pull_sheet_data(Sheets_SCOPES, Drive_SCOPES,SPREADSHEET_ID,RANGE_NAME):
sheet_creds = gsheet_api_check(Sheets_SCOPES)
drive_creds = drive_api_check(Drive_SCOPES)
sheet_service = build('sheets', 'v4', credentials=sheet_creds)
drive_service = build('drive', 'v3', credentials=drive_creds)
drive_service.files().copy(fileId=SPREADSHEET_ID, body={"mimeType":"application/vnd.google-apps.spreadsheet"}).execute()
#service.files().copy(fileId=SPREADSHEET_ID,convert=true, body={"title": "specifyName"}).execute()
sheet = sheet_service.spreadsheets()
copy_title = 'temp'
#result = copy_file(service, SPREADSHEET_ID, copy_title)
result = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
rows = sheet.values().get(spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME).execute()
data = rows.get('values')
print("COMPLETE: Data copied")
return data
Now I get the following error:
<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1bI_X2ssOj9gTYnKsa_CDajYP0FcFMr0E/copy?alt=json returned "Insufficient Permission: Request had insufficient authentication scopes.">
SPREADSHEET_IDis a .xlsx file? Or is it a Google Sheets file to which an .xlsx file has been converted? How did you perform the conversion?