title: How do I solve a Google Sheet API error in Python
body: <p>I'm using the following code to download a Gsheet into Python.  It works for some sheets but not all.</p>
  <pre><code>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(&quot;COMPLETE: Data copied&quot;)
          return data
  </code></pre>
  <p>On the sheets that cause an error I recieve the following msg:</p>
  <pre><code>HttpError: &lt;HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1bI_X2ssOj9gTYnKsa_CDajYP0FcFMr0E/values/3-7-14days?alt=json returned &quot;This operation is not supported for this document&quot;&gt;
  </code></pre>
  <p>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.</p>
  <p><strong>UPDATE</strong></p>
  <pre><code>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={&quot;mimeType&quot;:&quot;application/vnd.google-apps.spreadsheet&quot;}).execute()
      #service.files().copy(fileId=SPREADSHEET_ID,convert=true, body={&quot;title&quot;: &quot;specifyName&quot;}).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(&quot;COMPLETE: Data copied&quot;)
          return data
  </code></pre>
  <p>Now I get the following error:</p>
  <pre><code>&lt;HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1bI_X2ssOj9gTYnKsa_CDajYP0FcFMr0E/copy?alt=json returned &quot;Insufficient Permission: Request had insufficient authentication scopes.&quot;&gt;
  </code></pre>