Im trying to create an app for personal use that accesses the Gmail API to check for unread emails.
I created my own project in Google Cloud in order to get the necessary tokens to access the Gmail API, but only now I realized that the refresh token expires after a week and you have to re-authorize using the link.
I hate doing this, because I'm running the script on my raspberry pi, thus requiring me to ssh, somehow acquire the login link from the program running in the background and authorize. That gets old after some time.
Verifying my Google cloud project seems unfeasible, as you need to provide documentation and privacy policies. Is there anyway for me to access a non-expiring refresh token like you get when authorizing a published app? Why is it so dang hard to do with my account what I want to do? No manual option to actual get an API key?
I tried creating my own app in the Google cloud consoles and having it verified by google. I don't think it will actually go through.
For my case, I found out I could use IMAP to access my mail without the Gmail API.
This is done by having two-factor authentication enabled, and creating a so called app password
. This is a specific password that google creates and does (should!) not expire until you change your Google account password.
This app password
can then be used by simply accessing your Gmail using IMAP and the plaintext password:
import imaplib
import email
# Email account credentials
username = "your_email@gmail.com"
password = "your_app_password"
# Connect to Gmail's IMAP server
mail = imaplib.IMAP4_SSL("imap.gmail.com")
# Log in to your Gmail account
mail.login(username, password)