0

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 OAuth 2.0 for Server to Server Applications

here is my 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()

When I run this i get a an error 400 Bad Request.

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?

Thanks

HBizzle
  • 31
  • 1
  • 9
  • For debugging, did you try a real Gmail account first instead of service account? Try performing the request using Gmail accounts if it works. If it doesn't, then this is not doable with service accounts. Not sure if this is related to this [SO thread](http://stackoverflow.com/questions/29341096/gmail-rest-api-using-google-credentials-without-impersonate). – ReyAnthonyRenacia Nov 10 '16 at 08:45
  • Service accounts aren't going to work with Gmal unless this is a admin directory email, domain account, google for work account in that case you have to make sure the service account has access to the users gmail. Service accounts wont work with normal user gmail accounts. – Linda Lawton - DaImTo Nov 10 '16 at 12:31
  • @noogui what do you mean by a real Gmail account. Just to Clarify, I am using my own personal Gmail account to test this. I have used account services before to do the same thing and it has been working for 2 years, and all of sudden it just stopped, so I'm not sure if google changed something fundamental in their API or oAuth2 protocol. My original code was in java, that's the only difference. – HBizzle Nov 10 '16 at 17:11
  • what DalmTo said.. – ReyAnthonyRenacia Nov 10 '16 at 17:15
  • Is there another way to directly access personal gmail account through API without a human input requirement? – HBizzle Nov 14 '16 at 15:57

1 Answers1

0

You should to Enable your API on Dashboard in your API Manager menu here

Then, choose you interested in API

Finally, you'll see something like this

tubaros
  • 1
  • 1