Invalid omniauth oauth flags for gmail

I am trying to get the oauth token, which I can use with gmail_xauth (ruby gem) to view the user's mail. First I registered my application with google and then configure the application to request access to mail:

config.omniauth :google, 'key', 'secret', :scope => 'https://mail.google.com/mail/feed/atom/' 

Then I look through the outh / openid stream, and google tells me to approve gmail access, redirect me back to the application with a token and secret in my omniuth credentials, and my Google account lists my application as allowed to access my data. So far, so good.

Now when I take these credentials and try to use them with gmail_xoauth I like this:

  require 'gmail_xoauth' imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false) imap.authenticate('XOAUTH', '...@gmail.com', :consumer_key => 'key, :consumer_secret => 'secret', :token => 'omniauth_returned_token', :token_secret => 'omniauth_returned_secret' ) 

I get the error "Net :: IMAP :: NoResponseError: invalid credentials (Failure)".

Interestingly, after gmail_xoauth README to create a token with the same consumer using a python script, it works.

+8
ruby oauth gmail devise omniauth
source share
2 answers

This works for me:

 config.omniauth :google, 'anonymous', 'anonymous', :scope => 'https://mail.google.com/' 

I use gmail gem, so to connect it looks like this:

 gmail = Gmail.connect(:xoauth, auth.uid, :token => auth.token, :secret => auth.secret, :consumer_key => 'anonymous', :consumer_secret => 'anonymous' ) 

I am passing an authentication object, but you get it from the env env variable ["omniauth.auth"] . I use an anonymous / anonymous key / secret since I have not registered my domain with google, but I believe that you can here . It will still work with anonymous / anonymous, but Google will just alert the user.

+5
source share

The Google OAuth1 protocol is now outdated, and many gems have not yet been updated to use their OAuth2 protocol. The following is a working example of receiving email from Google using the OAuth2 protocol. This example uses mail , gmail_xoauth , omniauth and omniauth-google-oauth2 gems.

You also need to register your application in the Google API console in order to receive your API tokens.

 # in an initializer: ENV['GOOGLE_KEY'] = 'yourkey' ENV['GOOGLE_SECRET'] = 'yoursecret' Rails.application.config.middleware.use OmniAuth::Builder do provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], { scope: 'https://mail.google.com/,https://www.googleapis.com/auth/userinfo.email' } end # ...after handling login with OmniAuth... # in your script email = auth_hash[:info][:email] access_token = auth_hash[:credentials][:token] imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false) imap.authenticate('XOAUTH2', email, access_token) imap.select('INBOX') imap.search(['ALL']).each do |message_id| msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822'] mail = Mail.read_from_string msg puts mail.subject puts mail.text_part.body.to_s puts mail.html_part.body.to_s end 
+3
source share

All Articles