Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

omniauth oauth tokens for gmail are invalid

I'm trying to get an oauth token I can use with gmail_xauth (ruby gem) to look at a user's mail. I first registered my app with google and then set up devise to request access to mail:

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

I then go through the outh/openid flow and google prompts me to approve access to gmail, redirecting me back to the app with a a token and secret in the omniuth credentials & my Google account lists my app as authorized to access my data. So far so good.

Now, when I take those credentials and try to use them with gmail_xoauth like so:

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

I get an error "Net::IMAP::NoResponseError: Invalid credentials (Failure)".

Interestingly, following the gmail_xoauth README to generate a token with an same consumer using a python script it does work.

like image 611
Parker Thompson Avatar asked Apr 11 '11 00:04

Parker Thompson


1 Answers

This works for me:

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

I'm using the 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'm passing an authentication object in, but you'll be getting it from the env variable env["omniauth.auth"]. I'm using anonymous/anonymous for the key/secret since I haven't registered my domain with google, but I believe you can here. It'll still work with anonymous/anonymous, but Google will just warn the user.

like image 78
Jack Chu Avatar answered Oct 16 '22 01:10

Jack Chu