总共弄了 twitter,qq,weibo,三个三方认证 用的代码都是一样的
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.nickname
user.image = auth.info.image
end
end
def all
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash.notice = "Signed in!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
alias_method :weibo, :all
alias_method :twitter, :all
alias_method :qq_connect, :all
devise 的 initializer 里面也设置好了
config.omniauth :twitter, CONFIG[:twitter_consumer_key], CONFIG[:twitter_consumer_secret]
config.omniauth :weibo, CONFIG[:weibo_key], CONFIG[:weibo_secret]
config.omniauth :qq_connect, CONFIG[:qq_key], CONFIG[:qq_secret]
但是始终只有 twitter 能用,验证完以后自己填 email(因为本身 User Model 里面需要 email) weibo 的话,是提示 something went wrong。 QQ 则是授权完毕以后,转到了 sign_up 页面提示提示 Invalid credentials 主要的 code 都是按照 railscasts 上面写的 真的也不知道怎么 debug 了…… 因为我觉得用的代码都是一样的诶……(唯一就是 twitter 站点里面填的 callbacks URL 是 127.0.0.1 这个,QQ 和微博都是填的本身的 heroku 的网站,但是这个应该是没有影响的)。然后就是 weibo 和 QQ 都是在审核阶段,用的测试账号,还不能公开使用第三方登陆
weibo 的 heroku logs
2013-03-25T12:21:55+00:00 app[web.1]: (weibo) Request phase initiated.
2013-03-25T12:21:55+00:00 heroku[router]: at=info method=GET path=/users/auth/we
ibo host=issuethoughts.herokuapp.com fwd="175.8.21.241" dyno=web.1 queue=0 wait=
0ms connect=1ms service=10ms status=302 bytes=239
2013-03-25T12:21:56+00:00 app[web.1]: Started GET "/users/auth/weibo/callback?st
ate=aa27eee9443e8e5ccc96d024660939970b5ccc7a0d0c30df&code=083a9b5dd0035245c5b147
a3e5662af2" for 175.8.21.241 at 2013-03-25 12:21:56 +0000
2013-03-25T12:21:56+00:00 app[web.1]: (weibo) Callback phase initiated.
2013-03-25T12:21:58+00:00 app[web.1]: : SELECT "users".* FROM "users" WHERE "u
sers"."provider" = 'weibo' AND "users"."uid" = 1695469591 LIMIT 1):
2013-03-25T12:21:58+00:00 app[web.1]: app/controllers/omniauth_callbacks_contr
oller.rb:13:in `all'
2013-03-25T12:21:58+00:00 app[web.1]:
2013-03-25T12:21:58+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::Error:
ERROR: operator does not exist: character varying = integer
2013-03-25T12:21:58+00:00 app[web.1]: LINE 1: ...RE "users"."provider" = 'weibo'
AND "users"."uid" = 16954695...
2013-03-25T12:21:58+00:00 app[web.1]:
^
2013-03-25T12:21:58+00:00 app[web.1]: HINT: No operator matches the given name
and argument type(s). You might need to add explicit type casts.
2013-03-25T12:21:58+00:00 app[web.1]: app/models/user.rb:23:in `from_omniauth'
2013-03-25T12:21:58+00:00 app[web.1]: Parameters: {"state"=>"aa27eee9443e8e5cc
c96d024660939970b5ccc7a0d0c30df", "code"=>"083a9b5dd0035245c5b147a3e5662af2"}
2013-03-25T12:21:58+00:00 app[web.1]:
2013-03-25T12:21:58+00:00 app[web.1]:
2013-03-25T12:21:58+00:00 app[web.1]: Processing by OmniauthCallbacksController#
weibo as HTML
2013-03-25T12:21:58+00:00 app[web.1]: Completed 500 Internal Server Error in 5ms
QQ 的 heroku logs
heroku logs
2013-03-25T11:57:32+00:00 app[web.1]: (qq_connect) Callback phase initiated.
2013-03-25T11:57:34+00:00 app[web.1]: (qq_connect) Authentication failure! inval
id_credentials: OAuth2::Error, :
2013-03-25T11:57:34+00:00 app[web.1]: Processing by OmniauthCallbacksController#
failure as HTML
2013-03-25T11:57:34+00:00 app[web.1]: Completed 302 Found in 1ms (ActiveRecord:
0.0ms)
2013-03-25T11:57:34+00:00 app[web.1]: Parameters: {"code"=>"2BE57603894A6F97FC
28023BD975EF03", "state"=>"75453ef674f41b99f6ba9e84e9557733c80acca3b36951b2"}
2013-03-25T11:57:34+00:00 app[web.1]:
2013-03-25T11:57:34+00:00 app[web.1]: callback( {"error":100002,"error_descripti
on":"param client_secret is wrong or lost "} );
2013-03-25T11:57:34+00:00 app[web.1]: Redirected to http://issuethoughts.herokua
pp.com/users/sign_in
2013-03-25T11:57:34+00:00 heroku[router]: at=info method=GET path=/users/auth/qq
_connect/callback?code=2BE57603894A6F97FC28023BD975EF03&state=75453ef674f41b99f6
ba9e84e9557733c80acca3b36951b2 host=issuethoughts.herokuapp.com fwd="175.8.21.24
1" dyno=web.1 queue=0 wait=0ms connect=2ms service=1500ms status=302 bytes=114
2013-03-25T11:57:34+00:00 app[web.1]: Started GET "/users/sign_in" for 175.8.21.
241 at 2013-03-25 11:57:34 +0000
2013-03-25T11:57:34+00:00 app[web.1]: Processing by Devise::SessionsController#n
ew as HTML
2013-03-25T11:57:34+00:00 app[web.1]: Rendered devise/shared/_links.erb (1.2ms
)
2013-03-25T11:57:34+00:00 app[web.1]: Rendered layouts/_header.html.erb (1.1ms
)
2013-03-25T11:57:34+00:00 app[web.1]: Rendered devise/sessions/new.html.erb wi