8
votes

I have installed omniauth 1.0. Also I have oauth-0.4.5, oauth2-0.5.1, omniauth-facebook-1.0.0, omniauth-twitter-0.0.6.

omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :developer unless Rails.env.production?
  provider :facebook, ENV['167257285348131'],     ENV['c8c722f697scb2afcf1600286c6212a9'],     :scope => 'email,offline_access,read_stream', :display => 'popup'
  provider :twitter, ENV['fma2L22ObJCW52QrL7uew'], ENV['4aZfhCAOdiS7ap8pHJ7I1OZslFwVWWLiAMVpYUI']

end

session_controller.rb
class SessionsController < ApplicationController
require 'omniauth-facebook'
require 'omniauth-twitter'
require 'omniauth'

def create
    @user = User.find_or_create_from_auth_hash(auth_hash)
    self.current_user = @user
    redirect_to '/'
end



def auth_hash
request.env['omniauth.auth']
end

end

Also I add 'omniauth' 'omniauth-facebook' 'omniauth-twitter' gems to gemfile

There are two problems:

  1. When I go http://localhost:3000/auth/facebook I get { "error": { "message": "Missing client_id parameter.", "type": "OAuthException" } }

And the link graph.facebook.com/oauth/authorize?response_type=code&client_id=&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Ffacebook%2Fcallback&parse=query&scope=email%2Coffline_access%2Cread_stream&display=popup And there is no client_id!!!

  1. When I go to http://localhost:3000/auth/twitter I get OAuth::Unauthorized

401 Unauthorized

Any ideas?

3
have you solved the facebook error? If it is, can I get the work around? I get same error message. It can be solved if I added manually the client_id to query string parametersVina
@Vina ENV[] breaks it. Try to use "" only. For ex.: provider :facebook, '167257285348131', 'c8c722f697scb2afcf1600286c6212a9'Alex D.
Thanks, it work! It seems I also need to restart the webrick. :DVina
I was having this exact same problem! Do you know why ENV[] breaks this? In my case it works fine with facebook but not with twitter.Ashitaka
try to source ~/.profile (or wherever you have defined the env variables) before starting the server. you need to do this only once.beno1604

3 Answers

15
votes

Alex D. is correct in that the ENV[] breaks it. To create omniauth.rb so that it uses different keys in different environments just put:

provider :twitter, TWITTER_KEY, TWITTER_SECRET

in omniauth.rb

and then in your environment config files (config/environments/development.rb, etc.) put the key you want to use for that environment.

config/environments/development.rb:

TWITTER_KEY = 'aaaaaaa'
TWITTER_SECRET = 'aaaabbbbbb'

config/environments/production.rb:

TWITTER_KEY = 'ccccccc'
TWITTER_SECRET = 'ccccdddddd'
2
votes

ENV['something']

looks into your environment vars for "something", so it would expect

something='12345'

so you should do it like that

export AUTH_FB_KEY='....'
export AUTH_FB_SECRET='...'

check with

env

and update your config

provider :facebook, ENV['AUTH_FB_KEY'], ENV['AUTH_FB_SECRET']

if you use heroku

heroku config:add AUTH_FB_KEY='....'
0
votes

There have been breaking changes made in omniauth 1.0 - https://github.com/intridea/omniauth

OmniAuth 1.0 has several breaking changes from version 0.x. You can set the dependency to ~> 0.3.2 if you do not wish to make the more difficult upgrade. See the wiki for more information.

I would try reverting omniauth to 0.3.2:

gem install omniauth --version '~> 0.3.2'

or if you're using bundler, in your Gemfile:

gem omniauth, '~> 0.3.2'