使用fb_graph gem在facebook中分享内容

时间:2013-02-26 06:14:53

标签: ruby-on-rails facebook access-token sharing fb-graph

我正在尝试在使用fb_graph(2.6.0)gem创建内容时分享facebook,

我在尝试时遇到以下错误:

app = FbGraph::Application.new(APP_ID, :secret => APP_SECRET)
app.get_access_token
user = FbGraph::User.me(access_token).fetch

在开发控制台中

 => 377095675642525|X3SyfucEUnahEEFAFwkO_cFXRyc 
 1.9.3p194 :005 > user = FbGraph::User.me(access_token).fetch
 LoadError: cannot load such file -- {"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.1.10/lib/active_support/dependencies.rb:234:in `load'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.1.10/lib/active_support/dependencies.rb:234:in `block in load'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.1.10/lib/active_support/dependencies.rb:223:in `block in load_dependency'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.1.10/lib/active_support/dependencies.rb:640:in `new_constants_in'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.1.10/lib/active_support/dependencies.rb:223:in `load_dependency'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.1.10/lib/active_support/dependencies.rb:234:in `load'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/fb_graph-2.6.0/lib/fb_graph/node.rb:138:in `handle_response'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/fb_graph-2.6.0/lib/fb_graph/node.rb:49:in `get'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/fb_graph-2.6.0/lib/fb_graph/node.rb:19:in `fetch'
from (irb):5
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.1.10/lib/rails/commands/console.rb:45:in `start'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.1.10/lib/rails/commands/console.rb:8:in `start'
from /home/love/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.1.10/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

在Heroku日志中:

 Started GET "/facebook/callback?code=AQDccVXzo55GpSCCnr0vXKb2xpHFtJ8E_Eb-3AfA6Wcx5G1QEMHr4vNuH0366tGYVFepTuDZv2Z9_BLeXdMRNcLtfg1N3mz8rbTZowblEeGqg9Ls7R7X7P2GiuyFPJa0PTric7gjE8cZ3olRyP27otiGfzWQ4REoqS-BFsH4BwyfyZSiZxoQ0PPDUmlxeve4Cn9qAKzC2sYmEEbdTcPo2quu" for 122.164.241.1 at 2013-02-26 05:42:55 +0000
2013-02-26T05:42:56+00:00 app[web.1]: #<FbGraph::Auth:0x00000004e1bbb8 @client=#   <Rack::OAuth2::Client:0x00000004e19b38 @identifier=188451457933295, @secret="13d6158a6ebe35a115bcae7f0ec14826", @redirect_uri=nil, @scheme=nil, @host="graph.facebook.com", @port=nil, @authorization_endpoint="/oauth/authorize", @token_endpoint="/oauth/access_token", @grant=#  <Rack::OAuth2::Client::Grant::ClientCredentials:0x00000005484e28>>>
app/controllers/facebooks_controller.rb:29:in `create'
2013-02-26T05:42:56+00:00 heroku[router]: at=info method=GET path=/facebook/callback?code=AQDccVXzo55GpSCCnr0vXKb2xpHFtJ8E_Eb-3AfA6Wcx5G1QEMHr4vNuH0366tGYVFepTuDZv2Z9_BLeXdMRNcLtfg1N3mz8rbTZowblEeGqg9Ls7R7X7P2GiuyFPJa0PTric7gjE8cZ3olRyP27otiGfzWQ4REoqS-BFsH4BwyfyZSiZxoQ0PPDUmlxeve4Cn9qAKzC2sYmEEbdTcPo2quu host=x.y.com fwd="122.164.241.1" dyno=web.1 queue=0 wait=0ms connect=2ms service=431ms status=500 bytes=0

在Facebook上点击登录后,我在浏览器中出现以下错误

Server error
The website encountered an error while retrieving     http://x.y.com/facebook/callback?code=AQAGIeV8GLZWMI98eauog2eTDq7jeXBEhBSDg3izqBBjS6mVN3y5tgTSGcagmAqMTYHyB_EJArN834cglLjBL4yHDQHlEaD7j7wSmSPN43hZXQhCHJTbvP0HHJPBwAiNCCZYOiP2jpLLAyznLoufwvN-6p6dLgjup8WPtRyEeUspv94m-O5lC146JHNvWFUvCHaPaQ2wPWyfmh5C89mR2G0R#_=_. It may be down for maintenance or configured incorrectly.

1 个答案:

答案 0 :(得分:0)

从您的问题看来,您似乎正在使用应用程序访问令牌来访问用户信息。

中的重新开始
FbGraph::InvalidRequest: OAuthException :: An active access token must be used to query information about the current user.

您应该使用用户的访问令牌来获取个人资料信息或在墙上发帖。

如果您尝试使用Facebook进行omniauth登录,这里有一个很好的入门教程http://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/

使用fb_graph时您应该拥有用户的访问令牌来执行任何进一步的操作,例如更新状态(墙贴)

这需要一个访问令牌,在为该用户验证应用时,您应该保存该访问令牌。

您可以

me = FbGraph::User.me(ACCESS_TOKEN)
me.feed!(
  :message => 'Updating via FbGraph',
  :picture => 'https://graph.facebook.com/matake/picture',
  :link => 'https://github.com/nov/fb_graph',
  :name => 'FbGraph',
  :description => 'A Ruby wrapper for Facebook Graph API'
)

注意:这需要您可以在范围属性

中指定的扩展权限
Rails.application.config.middleware.use OmniAuth::Builder do
  # The following is for facebook
  provider :facebook, [APP ID], [SECRET KEY], {:scope =&gt; 'email, read_stream, read_friendlists, friends_likes, friends_status, offline_access'}

  # If you want to also configure for additional login services, they would be configured here.
end
相关问题