简体   繁体   中英

Sharing things in facebook using fb_graph gem

I'm trying to share in facebook while creating the content using fb_graph(2.6.0) gem,

i'm getting following errors while trying :

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

In Development console

 => 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>'

In Heroku logs :

 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

After clicking login in facebook i'm getting following error in browser,

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.

From your question it seems you are using applications access token to access user info. That reslts in

FbGraph::InvalidRequest: OAuthException :: An active access token must be used to query information about the current user.

Whereas you should be using user's access token to fetch the profile info or post on your wall.

In case you are trying to do omniauth login with Facebook, here's a nice tutorial to get started http://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/

When using fb_graph You should be have user's access token to carry out any further actions for example to Update status (wall post)

This requires an access token, which you should be saving while authenticating you app for that user.

You can do

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'
)

Note: This would require extended permissions which you can specify in scope attribute

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM