Search code examples
oauth-2.0oauthcrongoogle-oauthgmail-api

Laravel scheduler with task that requires OAuth2


I am using Gmail API to retrieve recent messages. And of course Gmail API requires OAuth2 Token to authenticate the requests. And repeat the task indefinitely every nth time.

However, I think that this is an impossible task. Because OAuth token expires. Though it has a refresh token, It will still need initial user intervention to start the task.

Does anyone experience the same problem? If so, how did you guys overcome it?

I'm kinda stuck on this matter. and would love to hear a solution.


Solution

  • I am using Gmail API to retrieve recent messages.

    Your trying to access private user data you need the consent of the user to do that.

    this is an impossible task. Because OAuth token expires.

    Tokens expire this is intentional if they didn't and someone got your token they could use it for ever by having an expiration time on the token this limits how long a hacker would have access to your data.

    Does anyone experience the same problem? If so, how did you guys overcome it?

    These are things you should not be trying to over come these are things you should accept and try to understand the security they bring to your application.

    I'm kinda stuck on this matter. and would love to hear a solution.

    If this is a google workspace domain account, you could consider using a service account.

    However if this is a standard google gmail user then you will need to use Oauth2 and request the consent of the user. If you have a refresh token you should not be having an issue you just need to authorize the user once and you will be able to request a new access token when ever you need.