Search code examples
ruby-on-railsrubyherokugmailgmail-api

Gmail::Client::AuthorizationError only in Production for Rails App on Heroku


I have a RoR app that reads emails from my inbox using the gmail gem. I've deployed to Heroku and everything works fine, except connecting to gmail.

On my local machine it connects with no issues (after I allowed access for less secure apps).

Using the basic gmail login method;

Gmail.connect!('[email protected]','password')

I get the following error in production only.

Gmail::Client::AuthorizationError: Couldn't login to given Gmail account: [email protected] (Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure))

And then I'll go to https://www.google.com/accounts/DisplayUnlockCaptcha (as other answers have suggested) and it will work fine for a short time period and then suddenly stop working again.

I'm using Ruby v2.4.1, RoR v5.1.1 and the gmail gem v0.6.0 (https://github.com/gmailgem/gmail)

Any help would be great.


Solution

  • Are you deploying to a domain? It could be caused by the fact that:

    Heroko will not give you even a range of IP addresses - they can, may and will move dynos between Amazon zones as needs require.

    Your only option would be some sort of proxy node with a static IP that they talk to that securely communicates to your Heroku app - or consider if Heroku is the right fit for you here altogether.

    Source: Get a finite list of IP addresses for my Heroku App?

    It seems like you sign into Google and whitelist what an IP (as far as they're concerned) and then the dyno switches a bit later for whatever reason so then Google no longer has your dyno's IP in the whitelist for this app.