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.

1
  • You can check the main method of the Python Quickstart. A service is created there. Commented Mar 1, 2016 at 21:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
Check this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.