title: GMAIL API: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/myemail%40gmail.com/labels?alt=json returned "Bad Request">
body: <p>I am trying to write a python program that will access my gmail account through the gmail API using account services. I followed all the steps in the Using <a href="https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests" rel="nofollow noreferrer">OAuth 2.0 for Server to Server Applications</a></p>
  
  <p>here is my code:</p>
  
  <pre><code>from __future__ import print_function
  import httplib2
  import os
  
  from apiclient.discovery import build
  from httplib2 import Http
  from oauth2client import client
  from oauth2client import tools
  from oauth2client.file import Storage
  from oauth2client.service_account import ServiceAccountCredentials
  
  try:
      import argparse
      flags = argparse.ArgumentParser(parents=[ tools.argparser]).parse_args()
  except ImportError:
     flags = None
  
  def main():
     SCOPES = ['https://www.googleapis.com/auth/gmail.modify']
     APPLICATION_NAME = 'Gmail Application'
     credentials =    ServiceAccountCredentials.from_json_keyfile_name(
          'MyKeyFile.json', scopes=SCOPES)
  
     http_auth = credentials.authorize(Http())
     service = build('gmail', 'v1', http=http_auth)
     results = service.users().labels().list(userId='myemail@gmail.com').execute()
     labels = results.get('labels', [])
  
     if not labels:
        print('No labels found.')
     else:
        print('Labels:')
        for label in labels:
          print(label['name'])
  
  
  if __name__ == '__main__':
  main()
  </code></pre>
  
  <p>When I run this i get a an error 400 Bad Request. </p>
  
  <p>Anyone encountered this issue. I have been changing things around foro a couple of days now and nothing has worked: I used the service ID instead of my email, I used  p12 credentials api, created new projects and new account services with new keys... you name it. I'm hitting a wall. Can anyone help? </p>
  
  <p>Thanks</p>