豆瓣API Rails 插件

目前只支持Rails 3 beta,地址:http://github.com/yzhang/douban

安装

$ gem install douban

使用

1. 修改Gemfile:


gem 'douban', :require => 'oauth'
require 'douban'

2. 运行generator

$ script/rails generate douban

3. 修改config/douban.yml, 并在ApplicationController中包含DoubanHelper:


class ApplicationController
  include DoubanHelper
end

4. 然后在你的application layout中加入:


<% if douban_authorized? %>
  <%= link_to '注销豆瓣登录', douban_logout_path %>
<% else %>
  <%= link_to "使用豆瓣帐号登录", douban_login_path %>
<% end %>

还可以使用下面的Helper:

  • douban_auth_or_login_required, 方便和RESTFUL_AUTH一起使用
  • douban_auth_required, 可以作为before_filter使用

5. 要访问用户资源,可以调用:


douban.get('/people/yzhang')
douban.post('/reviews', review)

6. 要验证用户是否已授权,可以调用:


douban.authorized?
=> true # 如果已经获得access token

This entry was posted in 插件. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • danish

    你好。我按照步骤,在访问的http://127.0.0.1:3000/douban/new 后出现错误。

    NoMethodError
    in DoubanController#new

    undefined method `new’ for Douban:Module

    app/controllers/douban_controller.rb:3:in `new’其中这一行是 douban = Douban.new我使用的是rails 3.0.9 已经安装了douban这个gem。请问这是哪儿出了问题。谢谢

无觅相关文章插件,快速提升流量