Search code examples
node.jsgmailgoogle-oauthgmail-api

How do i setup a service that collects users credentials authenticate the user with google gmail api and fetch mails from their inbox


I am building a service where I need to fetch certain messages from different users' Gmail inboxes. I have searched for a way to do this, only to keep stumbling on google OAuth flow. Is there a way I can authenticate with Gmail API, without the OAuth flow, and access different users inbox and fetch the messages? Because I won't be needing a GUI as this service will run on a server.


Solution

  • The first thing you need to understand is the diffrence between private and public data. Public data is data that is data that you can access without permisison. For example public videos uploaded to YouTube. Google holiday calendars on Google calendar.

    private data is data that is owned by a user, to access that data you need the permission of a user with access.

    To access some ones personal Gmail email account you need their permission the only way to get that permission is to use Oauth2. YOu will need to use oauth2 to request consent of the user once the user has consented if you also requested offline access you will get a refresh token. The refresh token can then be used by your backend system to request a new access token when ever it needs to. This access token can then be used to access the Gmail api.

    As you have stated these are standard gmail accounts you want to access. There is no other option. A user must grant you access to their data.