Ruby: Google Drive - Error=BadAuthentication (GoogleDrive::AuthenticationError) Info=InvalidSecondFactor
I've been using the Google Drive gem to try and interact with my Google Drive account and almost immediately ran into problems trying to login.
I started out with the following code:
require "rubygems" require "google_drive" session = GoogleDrive.login("firstname.lastname@example.org", "mypassword")
I'll move it to use OAuth when I put it into my application but for spiking this approach works. Unfortunately I got the following error when running the script:
/Users/markneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/google_drive-0.3.10/lib/google_drive/session.rb:93:in `rescue in login': Authentication failed for email@example.com: Response code 403 for post https://www.google.com/accounts/ClientLogin: Error=BadAuthentication (GoogleDrive::AuthenticationError) Info=InvalidSecondFactor from /Users/markneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/google_drive-0.3.10/lib/google_drive/session.rb:86:in `login' from /Users/markneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/google_drive-0.3.10/lib/google_drive/session.rb:38:in `login' from /Users/markneedham/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/google_drive-0.3.10/lib/google_drive.rb:18:in `login' from src/gdoc.rb:15:in `<main>'
Since I have two factor authentication enabled on my account it turns out that I need to create an app password to login:
It will then pop up with a password that we can use to login (I have revoked this one!):
We can then use this password instead and everything works fine:
require "rubygems" require "google_drive" session = GoogleDrive.login("firstname.lastname@example.org", "tuceuttkvxbvrblf")