当混合omniauth和fbgraph gems时,我无法获得访问密钥

时间:2011-06-29 18:14:18

标签: ruby-on-rails facebook omniauth

我正在使用Rails 3。 我在尝试获取访问密钥时在SessionsController中创建了一个Rack :: OAuth2 :: Client :: Error错误。 我使用omniauth登录facebook,我得到了param [“code”] 现在我尝试使用fbgraph来获取访问代码,我得到了Rac :: OAuth2 :: Client :: Error。

  def create
    auth=request.env["omniauth.auth"]
    fb_auth=FbGraph::Auth.new("xxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxx")
    client=fb_auth.client
    client.redirect_uri="http://localhost:3000/facebook/callback/"
    client.authorization_code=params[:code]
    access_token = client.access_token!  # => Rack::OAuth2::AccessToken
    user=User.find_by_provider_and_uid(auth["provider"], auth["id"]) ||User.create_with_omniauth(auth, access_token)
    session[:user_id]=user.id

    redirect_to root_url, :notice => "Signed In!"
  end

access_token! line是返回错误的原因。

1 个答案:

答案 0 :(得分:1)

如果你在fb_auth上设置redirect_uri而不是客户端,那就应该没问题了。

fb_auth = FbGraph::Auth.new(
   "xxxxxxx",
   "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
   :redirect_uri = "http://localhost:3000/facebook/callback"
)
client=fb_auth.client
client.authorization_code=params[:code]
access_token = client.access_token!  # => Rack::OAuth2::AccessToken

您还应该查看https://github.com/nov/fb_graph/issues/127#issuecomment-2244499, 然后读取fb_graph_sample代码并设置你的FbGraph调试。