title: Rails & Omniauth: Twitter /auth/failure?message=invalid_response
body: <p>I'm using <code>omniauth</code> to implement twitter authentication in my application. I have facebook authentication working, and I know where the code for twitter authentication fails, but I can't figure out why!</p>
  
  <p>Twitter does not provide an email address, and my site authenticates based on email/pw, so I only allow users to add a Twitter account to a pre-existing account, created by signing-up on the site or through facebook.</p>
  
  <p>This is the relevant code, part of my <code>services_controller.rb</code>:</p>
  
  <pre><code>logger.info("USER SIGNED IN")
  auth = Service.find_by_provider_and_uid(provider, uid)
  logger.info("AUTH: #{auth}")
  #If user is signed in but does not have this service linked to his account
  if !auth
    logger.info("AUTH IS NULL, SERVICE NOT LINKED TO ACCOUNT")
    logger.info("PROVIDER: #{provider}, UID: #{uid}, EMAIL: #{email}")
    user.services.create!(:provider =&gt; provider, :uid =&gt; uid, :uemail =&gt; email)
    logger.info("SERVICE CREATED")
    flash[:notice] = 'Sign in via ' + provider.capitalize + ' has been added to your account.'
    redirect_to services_path
  </code></pre>
  
  <p>Now, I the last thing I see in my log is:</p>
  
  <pre><code>AUTH IS NULL, SERVICE NOT LINKED TO ACCOUNT
  PROVIDER: twitter, UID: 3195xxxxx, EMAIL: 
  Completed   in 231ms
  Started GET "/auth/failure?message=invalid_response" for 127.0.0.1 at 2011-08-05 20:52:30 +0530
  ActionController::RoutingError (No route matches "/auth/failure"):
  </code></pre>
  
  <p>As you can see from the code above, this means that the line <code>user.services.create!(...)</code> fails, which is why I never see <code>SERVICE CREATED</code> in my log. I don't know why it fails though. I know the <code>email</code> variable is empty, but that is expected since Twitter does not provide emails. I have even tried replacing <code>:uemail =&gt; email</code> with <code>:uemail =&gt; "validemail@gmail.com"</code>, but I still get the same output. </p>