在参照:

http://rubydoc.info/github/arsduo/koala/master/Koala/Facebook/GraphAPIMethods#graph_call-instance_method

http://developers.facebook.com/roadmap/offline-access-removal/#extend_token

我的代码看起来像这样:

fb = Koala::Facebook::API.new(access_token)
fb.graph_call("/oauth/access_token", {"client_id" => app_id, "client_secret" => app_secret, "grant_type" => "fb_exchange_token", "fb_exchange_token" => access_token })

虽然返回值包含扩展 token ,但它会抛出MultiJson::DecodeError
MultiJson::DecodeError: 387: unexpected token at 'access_token=long-lived_token&expires=5184000]'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/json-1.6.1/lib/json/common.rb:148:in `parse'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/json-1.6.1/lib/json/common.rb:148:in `parse'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/multi_json-1.0.3/lib/multi_json/engines/json_gem.rb:13:in `decode'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/multi_json-1.0.3/lib/multi_json.rb:65:in `decode'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/koala-1.3.0/lib/koala/api.rb:61:in `api'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/koala-1.3.0/lib/koala/api/graph_api.rb:421:in `graph_call'
from (irb):7
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

我怀疑导致错误的结尾]。不知道这是我的Facebook错误还是处理不当。感谢有人可以帮助我。感谢!

我也尝试使用Net::HTTP.get(uri)代替。但是得到了下面的错误。
Errno::ECONNRESET: Connection reset by peer
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/protocol.rb:135:in `read_nonblock'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/protocol.rb:135:in `rbuf_fill'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/protocol.rb:126:in `readline'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:2211:in `read_status_line'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:2200:in `read_new'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:1183:in `transport_request'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:1169:in `request'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:1073:in `request_get'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:394:in `block in get_response'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:627:in `start'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:393:in `get_response'
from /usr/local/rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:370:in `get'
from (irb):11
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-1.9.2-p136@dotcloud/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

不确定是防火墙还是其他东西?

最佳答案

使用OAuth exchange_access_token_info方法:

oauth = Koala::Facebook::OAuth.new("consumer_token", "consumer_secret")
oauth.exchange_access_token_info("auth_token")

P.S.我也收到了您按照自己的方式做的错误。问题是Koala希望响应的主体采用JSON格式,但是在这种特殊情况下,它实际上是URL(查询字符串)格式的名称/值对,例如:
access_token=[...]&expires=[...]

关于ruby-on-rails - 尝试使用Koala gem扩展Facebook oauth token ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11027472/

10-09 04:29