如何从 Facebook javascript SDK 获取授权码

发布于 2024-12-11 19:05:27 字数 952 浏览 0 评论 0原文

我已经设置了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2024-12-18 19:05:27

以防万一其他人偶然发现这个问题,您在使用 Devise/omniauth 时不需要将任何参数传递给控制器​​...以下工作完美

$('#fb-connect').live 'click', ->
  FB.login ((response) ->
    if response.authResponse
      window.location = "/users/auth/facebook/callback
    else
      console.log "User cancelled login or did not fully authorize."
  ), scope: "email, offline_access"

  false

编辑

如果使用较小的版本或解决当参数和代码都抛出时抛出的异常缺少以下内联 js 与 onclick 一起工作。

<script>
  function fb_authorise(){
    FB.login(function(response) {
      if(response.authResponse) {
        window.location = "/users/auth/facebook/callback?signed_request=<%= params[:signed_request]%>"
      }
    }, {scope: "email, offline_access"});
  };
</script>

因此,为了回答您原来的问题,您使用了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

$('#fb-connect').live 'click', ->
  FB.login ((response) ->
    if response.authResponse
      window.location = "/users/auth/facebook/callback
    else
      console.log "User cancelled login or did not fully authorize."
  ), scope: "email, offline_access"

  false

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.

<script>
  function fb_authorise(){
    FB.login(function(response) {
      if(response.authResponse) {
        window.location = "/users/auth/facebook/callback?signed_request=<%= params[:signed_request]%>"
      }
    }, {scope: "email, offline_access"});
  };
</script>

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

葵雨 2024-12-18 19:05:27

使用客户端身份验证,您不需要使用“代码”。您拥有的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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文