How do I access the gmail account I own using the Gmail API?

I want to run a node script as a cronjob that uses the Gmail API to poll the gmail account that I own.

I follow these quick start instructions :

quickstart

I was stuck in the first step. When requesting credentials for a cron script, he tells me that "User data cannot be accessed from the platform without a user interface, because interaction requires a login to log in":

enter image description here

The docs are confusing in general and mention "service accounts", "OAuth" and other things - I can’t say which of them apply to my use case and which do not. I used many SaaS APIs, and a typical workflow is to log in to your account, get the API key and secret key and use them in your script to access the API. This does not seem to be the paradigm used by the Gmail API, so I would be grateful for any directions or links to clearer instructions.

+6
source share
2 answers

I also found that the documentation can sometimes be misleading, but what you want is actually pretty straight forward when you get it:

  • Register your application with Google and tell which APIs you want your application to have access to (in this case, only the Gmail API). This will give you two lines: client_id and client_secret (this is the contents of the client_secrets.json file above).
  • Since you are just going to write a script only for your own account, you do not need an http server. Oauth Playground will be enough. Click the "Settings" button in the upper right corner and use your own OAuth credentials.
  • Select the Gmail API in the API list and follow the steps described.

Now you have access_token and refresh_token , which you can use for unlimited use of the script!

+4
source

To fully understand the use of the Gmail API, through the Gmail API Overview it really will matter.

And you can also use these documents to create a Gmail application:

  • Client libraries are available for download in several languages ​​and simplify the execution of API requests.
  • A developer guide will help you better understand how to implement specific use cases.
  • The API link gives you detailed information about each resource and method in the Gmail API.

I hope this helps.

0
source

All Articles