通过OmniAuth传递参数 [英] Passing parameters through OmniAuth
问题描述
我需要将一些参数传递给回调操作.从源代码来看,OmniAuth应该将查询字符串添加到回调URL,但奇怪的是没有.当我打开
I need to pass some parameters to callback action. Judging from the source code, OmniAuth should add query string to callback URL but strangely it does not. When I open
/auth/facebook?from=partner
...然后重定向到Facebook,return_url
就是
...and get redirected to Facebook, return_url
is just
/auth/facebook/callback
...没有任何参数.
...without any parameters.
推荐答案
在解决了所有上述问题之后,我想出了关于 Facebook 的处理方法,默认情况下,该工具不会在 Facebook 中显示参数request.env["omniauth.auth"]
.
After struggling with all the above answers, I figured out what to do regarding Facebook, which by default does not display the params in request.env["omniauth.auth"]
.
所以-如果您在查询中使用查询字符串,则类似于以下内容:
So -- If you are using a query string for the callback, similar to something like this:
"/auth/facebook?website_id=#{@website.id}"
获取该website_id参数的唯一方法是使用request.env["omniauth.params"]
. 注意:请确保您使用的是omniauth.params而不是omniauth.auth -这使我绊了一会儿.
The only way to get that website_id param is by using request.env["omniauth.params"]
. NOTE: MAKE SURE YOU USE omniauth.params and not omniauth.auth -- this one tripped me up for a while.
然后,要进行测试,可以在控制器动作中进行检查(请注意RAISE行...):
Then, to test this out, you can inspect it within your controller action (notice the RAISE line...):
def create
raise request.env["omniauth.params"].to_yaml
# the rest of your create action code...
end
您应该在那里看到您的参数.伟大的.现在,回到您的控制器并删除该RAISE行.然后,您可以在控制器操作中按以下方式访问参数:
You should see your parameter there. Great. Now, go back to your controller and remove that RAISE line. Then, you can access the param as follows in your controller action:
params = request.env["omniauth.params"]
website_id = params["website_id"]
注意:在params ["website_id"]中,您需要使用引号而不是符号.
NOTE: in params["website_id"] you need to use quotes and NOT a symbol.
这篇关于通过OmniAuth传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!