Facebook 和 RoR:如何重定向到授权 URL?

发布于 2024-11-10 12:18:36 字数 863 浏览 4 评论 0 原文

我正在尝试将用户重定向到请求访问用户信息的权限的 URL。

我有一个表单的视图,其操作如下:

class AuthController < ApplicationController

   def auth
     @oauth = Koala::Facebook::OAuth.new("*id*", "*secret*", 'http://localhost:3000/auth/auth2') 
     address = @oauth.url_for_oauth_code(:permissions => "publish_stream")
     puts "address  #{address}"
     redirect_to address
     return  
   end

   def auth2

   end

end

stdout 上,地址显示为:重定向到 https://graph.facebook.com/oauth/authorize?client_id=*id*&redirect_uri=http://localhost:3000/auth/auth2&scope=publish_stream< /code>

如果我在浏览器中独立输入此 URL(而不是通过我的应用程序),我会进入应用程序请求权限的正确页面。然而,通过我的应用程序,我被带到一个显示 Facebook 徽标的页面,而不是请求权限的页面。

我希望看到权限页面,然后一旦用户允许权限,请求就会转发到我的redirect_uri。

I am trying to redirect the user to the URL which requests permission to access the user's information.

I have a view with a form whose action goes to the following:

class AuthController < ApplicationController

   def auth
     @oauth = Koala::Facebook::OAuth.new("*id*", "*secret*", 'http://localhost:3000/auth/auth2') 
     address = @oauth.url_for_oauth_code(:permissions => "publish_stream")
     puts "address  #{address}"
     redirect_to address
     return  
   end

   def auth2

   end

end

On stdout, address shows up as: Redirected to https://graph.facebook.com/oauth/authorize?client_id=*id*&redirect_uri=http://localhost:3000/auth/auth2&scope=publish_stream

If I enter this URL standalone in my browser (and not through my app), I get taken to the correct page where the app requests permissions. However, for through my application, I am taken to a page that shows the Facebook logo, not the page requesting permissions.

I expected to see the permissions page and then once the user allow's permission, the request forwards to my redirect_uri.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

花落人断肠 2024-11-17 12:18:36

您是使用 fbml 进行重定向还是尝试在服务器端进行重定向?如果您的应用程序是画布页面并且您正在重定向服务器端,则您只是重定向 facebook,而不是用户。您需要确保您实际上是在告诉 Facebook 进行重定向。

如果是这种情况,请查看 fb:redirect

Are you using the fbml for a redirect or trying to do it server side? If your app is a canvas page and you're redirecting server side, you are only redirecting facebook, not the user. You will need to ensure you are actually telling facebook to do the redirect.

Check out fb:redirect if that is the case.

御弟哥哥 2024-11-17 12:18:36

我只是想发布我的问题的答案。解决方案实际上非常简单。我不需要在控制器中重定向到授权 uri,而是需要在我的视图中执行如下操作。这避免了中间登录页面,并且似乎是其他遇到此特定问题的人正在做的事情(其中提供中间 facebook 页面,而不是将用户直接传递到授权页面。)

<script language=JavaScript>
top.location.href="https://graph.facebook.com/oauth/authorize?client_id=client_id&redirect_uri=http://localhost:3000/xxx&scope=publish_stream";
</script>

我不能说我理解为什么会这样很重要,我无法在控制器中重定向。因此,如果任何使用 RoR 的人可以解释原因,请告诉我。

I just wanted to post the answer to my question. The solution was actually quite simple. Instead of doing the redirect to the authorization uri in my controller, I needed to do it in my view as follows. This avoided the intermediate login page and seems to be what everyone else is doing who has hit this particular problem (where an intermediate facebook page is served instead of passing the user directly to the authorization page.)

<script language=JavaScript>
top.location.href="https://graph.facebook.com/oauth/authorize?client_id=client_id&redirect_uri=http://localhost:3000/xxx&scope=publish_stream";
</script>

I can't say I understand why it matters and I can't redirect in the controller. So if anyone using RoR can explain why, let me know.

一念一轮回 2024-11-17 12:18:36

我最近遇到了类似的问题。为了使您的应用程序脱离 iframe 并从 Rails 中的服务器端重定向重定向到授权页面,您可以执行以下操作:

def facebook_authorization
  redirect_url =<<-EOS
    <script>
      top.location.href='https://graph.facebook.com/oauth/authorize?client_id=client_id&redirect_uri=http://localhost:3000/xxx&scope=publish_stream';
    </script>
   EOS       

   render :inline => redirect_url
end

这与使用 php 的 facebook 文档中描述的方式几乎相同。

此外,如果用户授权您的应用程序,您将在signed_request响应参数中获得oauth_access令牌。我不使用 Koala,所以不确定他们如何解析签名的请求。

通过测试,我发现只要用户授权了您的应用程序,您就会在每次请求时从 facebook 获取 oauth_access 令牌。这样可以轻松检测用户是否已授权该应用程序,并且您可以避免在用户每次访问您的应用程序时向用户发送身份验证页面。

I recently encountered a similar problem. In order to break your app out of the iframe and redirect to authorization page from a server side redirect in rails you can do i.e.:

def facebook_authorization
  redirect_url =<<-EOS
    <script>
      top.location.href='https://graph.facebook.com/oauth/authorize?client_id=client_id&redirect_uri=http://localhost:3000/xxx&scope=publish_stream';
    </script>
   EOS       

   render :inline => redirect_url
end

This is pretty much the the same way as described in the facebook documentation using php.

Further if the user authorizes your app, you will get the oauth_access token in the signed_request response parameter. I don't use Koala so not sure how they parse the signed request.

From testing I have found that for as long as the user has authorized your app you will get the oauth_access token back from facebook on every request. This makes it easy to detect whether the user has authorized the app or not and you avoid sending the user the auth page every time he/she visits your app.

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