我已成功地将 OmniAuth Facebook 登录流程集成到服务器端的 rails 应用程序中。但是,我也试图在客户端使用 Facebook Javascript SDK 让它工作,并且遇到了一些问题。
编辑:这个问题似乎只发生在 Chrome 中,而不是在 Safari 或 Firefox 中
会话控制器- 这适用于服务器端流程
def create
auth = request.env['omniauth.auth']
#if an authorization does not exisit, it will create a new authorization record. it will also create a new user record if a user is not currently logged in
unless @auth = Authorization.find_from_hash(auth)
# Create a new user or add an auth to existing user, depending on
# whether there is already a user signed in.
@auth = Authorization.create_from_hash(auth, current_user)
#add the friends array to user record. as of now only doing this on the initial user create
@friends = []
FbGraph::User.me(@auth.user.authorization.facebook_token).fetch.friends.each do |t|
@friends << t.identifier
end
u = @auth.user
u.facebook_friends = @friends
u.save
end
#store a new auth token if needed (if the new token in the hash does not match the one stored in the database for authorization)
Authorization.check_if_new_auth_token_is_needed(auth)
# Log the authorizing user in.
self.current_user = @auth.user
redirect_to root_url
end
如果我只是点击 /auth/facebook 路径,用户将登录
路线
match '/auth/:provider/callback', :to => 'sessions#create'
现在在主页视图上,我正在尝试运行客户端流登录
主页视图
<script>
$(function() {
$('a').click(function(e) {
e.preventDefault();
FB.login(function(response) {
if (response.authResponse) {
$('#connect').html('Connected! Hitting OmniAuth callback (GET /auth/facebook/callback)...');
// since we have cookies enabled, this request will allow omniauth to parse
// out the auth code from the signed request in the fbsr_XXX cookie
$.getJSON('/auth/facebook/callback', function(json) {
$('#connect').html('Connected! Callback complete.');
$('#results').html(JSON.stringify(json));
});
}
}, { scope: 'email,publish_stream' });
});
});
</script>
<p id="connect">
<a href="#">Connect to FB</a>
</p>
<p id="results" />
我的日志中出现以下错误
{"error":{"message":"缺少授权码","type":"OAuthException","code":1}}
基本上,Omniauth 并没有收到来自 FB.login 操作的 facebook 签名请求(正如https://github.com/mkdynamic/omniauth-facebook/blob/master/example/config.ru所说的那样)。
关于如何让它正常工作或我可能做错了什么的任何想法?