如何从 Facebook javascript SDK 获取授权码
我已经设置了 devise/omniauth,现在我想使用 Facebook javascript SDK 登录/请求权限,然后将它们路由到我的omniauth 回调控制器。
这就是我所拥有的(咖啡脚本)。
$('#fb-connect').live 'click', ->
FB.login ((response) ->
if response.authResponse
window.location = "/users/auth/facebook/callback?code=" + response.authResponse.signedRequest
else
console.log "User cancelled login or did not fully authorize."
), scope: "email, offline_access"
false
但我收到验证码格式无效
错误。我假设这是因为代码参数需要除签名请求之外的其他内容?
更新
所以看起来我需要传递授权码,但我找不到如何传递。 直接网址示例显示您可以指定response_type=code来获取授权代码,但我不知道如何使用 FB.api 来做到这一点。有什么想法吗?
http://www.facebook.com/dialog/oauth/?
scope=email,user_birthday&
client_id=123050457758183&
redirect_uri=http://www.example.com/response&
response_type=code
I have devise/omniauth set up and now I would like to use the Facebook javascript SDK to login/ask for permissions and then route them to my omniauth callbacks controller.
This is what I have (coffeescript).
$('#fb-connect').live 'click', ->
FB.login ((response) ->
if response.authResponse
window.location = "/users/auth/facebook/callback?code=" + response.authResponse.signedRequest
else
console.log "User cancelled login or did not fully authorize."
), scope: "email, offline_access"
false
But I'm getting an Invalid verification code format
error. I'm assuming it's because the code param expects something other than the signed request?
Update
So it looks like I need to pass in the authorization code, but I can't find how. The direct url example shows that you can specify response_type=code to get the authorization code but I don't know how to do that using FB.api. Any ideas?
http://www.facebook.com/dialog/oauth/?
scope=email,user_birthday&
client_id=123050457758183&
redirect_uri=http://www.example.com/response&
response_type=code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以防万一其他人偶然发现这个问题,您在使用 Devise/omniauth 时不需要将任何参数传递给控制器...以下工作完美
编辑
如果使用较小的版本或解决当参数和代码都抛出时抛出的异常缺少以下内联 js 与 onclick 一起工作。
因此,为了回答您原来的问题,您使用了signed_request参数,然后使用code=notsigned_request添加到URL
Just incase anyone else stumbles across this question you don't need to pass any parameters to your controller when using Devise/omniauth... the following works perfectly
EDIT
If using a lesser version or to get around exception thrown when both param and code are missing the following inline js works with an onclick.
So in answer to your original question you were using the signed_request param and then adding to the URL with code= not signed_request
使用客户端身份验证,您不需要使用“代码”。您拥有的response.authResponse 将已经包含access_token。 FB.login() 已经为您完成了这一切。如果您有“window.location = ...”,则用户已登录。
检查身份验证上的文档 - 确保您正在阅读客户端部分。另请检查 fb.login 上的文档
With client side auth, you don't need to use 'code'. That response.authResponse you have will contain the access_token already. FB.login() has already done all this for you. Where you have "window.location = ...", the user is logged in.
Check the doc on authentication - make sure you are reading the client side section. Also check the doc on fb.login