Search code examples
google-apigoogle-oauthgmail-apiservice-accounts

How to use gmail api based on use case


I want to know how to use the gmail api correctly for my use case (after next paragraph) because I'm confused.

I've so far tried setting up OAuth with a project and credentials, associated redirect URLs, and I ran quickstart.py successfully. But, since I'm not needing access to anyone else's account, and a consent screen does not make sense for my app, I'm not sure if I'm heading in right direction.

Use case: One already-existing gmail account is the project admin account --

  1. user registers with the app via /signup
  2. app uses the account to send first email to the user for confirmation
  3. user clicks the received email link to confirm. That prompts...
  4. a second email to the admin who clicks a link to approve the user
  5. if admin approval happens, a third email is sent to the user as a welcome email; otherwise, deny email.

Since I'm only dealing with one account that I myself set up for project admin, and also based on this, I think I should be using a service account.

Is that correct?


Solution

  • In order to access private user data you need permission of that user to access their data. In this instance your application needs your permission to access your gmail account. The way your application requests consent is with Oauth2 and showing the consent screen.

    The fact that you need to authorize your application once shouldn't be to much of an issue. Just have your application store the refresh token and it will be able to request a new access token as needed.

    service accounts.

    If you have a google workspace account, and the gmail account you are accessing is on that domain. then you could and should be using a service account for your use case.

    You will need to configure domain wide delegation to the service account and allow it to impersonate you.

    If you are using .net then the sample that i posted in the question you have linked should work. for you. I can find you a service account example for python if you need.