1

I'm trying to understand and use the example on the Google Developers page for using the Gmail API. The function takes an argument "service", but I am unsure what it expects here.

def SendMessage(service, user_id, message):

Everything else seems self explanatory but I need help on what to provide for the service variable?

The comment given within the example defines "service" as "service: Authorized Gmail API service instance." Which unfortunately still doesn't help me.

I'm not new to Python, but I am new to using APIs.

  • You can check the main method of the [Python Quickstart](https://developers.google.com/gmail/api/quickstart/python#step_2_install_the_google_client_library). A service is created there. – Tholle Mar 01 '16 at 21:00

2 Answers2

4

The argument "service" does get created in the python quickstart example

service = discovery.build('gmail', 'v1', http=http)

That service value can be used in the example you listed in your original question.

Duppie
  • 143
  • 8
2

The docstring of the function gives us a better idea. Found here

 def SendMessage(service, user_id, message):
   """Send an email message.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.
    message: Message to be sent.

  Returns:
    Sent Message.
  """

If you do not know how get an authorized Gmail API service instance, you can find a pythong quickstart here.

In short, you have to make a call to the API, and you will receive a client.json file, which contains your authentication that you can use in your project.

mugabits
  • 1,015
  • 1
  • 12
  • 22
  • Thanks for the information. I made the call, and I have the client.json file. Unfortunately, I am new to using the API calls so I'm not entirely sure what to do with the file. In the meantime, I have this working using smtplib. But to do this I had to enable less secure connections, which is why I'd like to explore and get the API call working. – Jason Smith Mar 01 '16 at 23:39
  • Check this [answer](http://stackoverflow.com/questions/32136330/where-do-i-get-the-authorized-gmail-api-service-instance-python-gmail-api) – mugabits Mar 02 '16 at 02:53