from __future__ import print_function
import httplib2
import os
import sys

import pickle

from apiclient import discovery
from apiclient import errors
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

try:
   import argparse
   flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
   flags = None

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/gmail-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/gmail.labels'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Inbox Organize'

def get_credentials():
   """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
   home_dir = os.path.expanduser('~')
   credential_dir = os.path.join(home_dir, '.credentials')
   if not os.path.exists(credential_dir):
      os.makedirs(credential_dir)
   credential_path = os.path.join(credential_dir,
         'gmail-python-quickstart.json')

   store = Storage(credential_path)
   credentials = store.get()
   if not credentials or credentials.invalid:
      flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
      flow.user_agent = APPLICATION_NAME
      if flags:
         credentials = tools.run_flow(flow, store, flags)
      else: # Needed only for compatibility with Python 2.6
         credentials = tools.run(flow, store)
      print('Storing credentials to ' + credential_path)
   return credentials

def GetLabels(service, user_id):
   try:
      response = service.users().labels().list(userId=user_id).execute()
      labels = response['labels']
      """
      for label in labels:
         print ('Label id: %s - Label name: %s' % (label['id'], label['name']))
      """
      return labels
   except errors.HttpError as error:
      print ('An error occurred: %s' % error)

def DeleteLabel(service, user_id, label_id):
   try:
      service.users().labels().delete(userId=user_id, id=label_id).execute()
      print ('Label with id: %s deleted successfully.' % label_id)
   except errors.HttpError as error:
      print ('An error occurred: %s' % error)

def main():
   credentials = get_credentials()
   http = credentials.authorize(httplib2.Http())
   service = discovery.build('gmail', 'v1', http=http)

   userId = 'me'

   labels = GetLabels(service, userId)

   for label in labels:
      if (label['type'] == 'user'):
         print('Deleting label:', label['name'])
         DeleteLabel(service, userId, label['id'])

if __name__ == '__main__':
   main()


[((35, 14, 35, 37), 'os.path.expanduser', 'os.path.expanduser', ({(35, 33, 35, 36): '"""~"""'}, {}), "('~')", False, 'import os\n'), ((36, 20, 36, 58), 'os.path.join', 'os.path.join', ({(36, 33, 36, 41): 'home_dir', (36, 43, 36, 57): '""".credentials"""'}, {}), "(home_dir, '.credentials')", False, 'import os\n'), ((39, 21, 40, 40), 'os.path.join', 'os.path.join', ({(39, 34, 39, 48): 'credential_dir', (40, 9, 40, 39): '"""gmail-python-quickstart.json"""'}, {}), "(credential_dir, 'gmail-python-quickstart.json')", False, 'import os\n'), ((42, 11, 42, 35), 'oauth2client.file.Storage', 'Storage', ({(42, 19, 42, 34): 'credential_path'}, {}), '(credential_path)', False, 'from oauth2client.file import Storage\n'), ((76, 13, 76, 54), 'apiclient.discovery.build', 'discovery.build', (), '', False, 'from apiclient import discovery\n'), ((37, 10, 37, 40), 'os.path.exists', 'os.path.exists', ({(37, 25, 37, 39): 'credential_dir'}, {}), '(credential_dir)', False, 'import os\n'), ((38, 6, 38, 33), 'os.makedirs', 'os.makedirs', ({(38, 18, 38, 32): 'credential_dir'}, {}), '(credential_dir)', False, 'import os\n'), ((45, 13, 45, 71), 'oauth2client.client.flow_from_clientsecrets', 'client.flow_from_clientsecrets', ({(45, 44, 45, 62): 'CLIENT_SECRET_FILE', (45, 64, 45, 70): 'SCOPES'}, {}), '(CLIENT_SECRET_FILE, SCOPES)', False, 'from oauth2client import client\n'), ((75, 32, 75, 47), 'httplib2.Http', 'httplib2.Http', ({}, {}), '()', False, 'import httplib2\n'), ((16, 11, 16, 61), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (), '', False, 'import argparse\n'), ((48, 23, 48, 57), 'oauth2client.tools.run_flow', 'tools.run_flow', ({(48, 38, 48, 42): 'flow', (48, 44, 48, 49): 'store', (48, 51, 48, 56): 'flags'}, {}), '(flow, store, flags)', False, 'from oauth2client import tools\n'), ((50, 23, 50, 45), 'oauth2client.tools.run', 'tools.run', ({(50, 33, 50, 37): 'flow', (50, 39, 50, 44): 'store'}, {}), '(flow, store)', False, 'from oauth2client import tools\n')]