Skip to content

Instantly share code, notes, and snippets.

@saberma
Created July 16, 2011 10:24
Show Gist options
  • Save saberma/1086239 to your computer and use it in GitHub Desktop.
Save saberma/1086239 to your computer and use it in GitHub Desktop.
douban api ruby
# -----------------------------------------------------------------
# 本程序提供最基本的Douban OAuth认证的Ruby示例代码
# 更多其他语言版本的Douban OAuth认证示例代码在 http://code.google.com/p/douban-oauth-sample/ 上提供
# 有任何疑问,可以到 http://www.douban.com/group/dbapi/ 上提问
# -----------------------------------------------------------------
# Douban OAuth认证包括以下四步内容
#
# 1. 获取Request Token
# 2. 用户确认授权
# 3. 换取Access Token
# 4. 访问受限资源
require'oauth'
api_key = "" # 修改key
api_key_secret = "" # 修改secret
@consumer=OAuth::Consumer.new(
api_key,
api_key_secret,
{
:site=>"http://www.douban.com",
:request_token_path=>"/service/auth/request_token",
:access_token_path=>"/service/auth/access_token",
:authorize_path=>"/service/auth/authorize",
:signature_method=>"HMAC-SHA1",
:scheme=>:header,
:realm=>"http://yoursite.com"
}
)
puts "1. 获取Request Token"
@[email protected]_request_token
puts "2. 用户确认授权"
puts "请将下面url粘贴到浏览器中,并同意授权,同意后按任意键继续:"
puts @request_token.authorize_url
gets
puts "3. 换取Access Token"
@access_token=@request_token.get_access_token
# i should re-generate access_token proxy here,
# since ruby oauth library assume the domain of the auth site should be same with the resource site
@access_token = OAuth::AccessToken.new(
OAuth::Consumer.new(
api_key,
api_key_secret,
{
:site=>"http://api.douban.com",
:scheme=>:header,
:signature_method=>"HMAC-SHA1",
:realm=>"http://yoursite.com"
}
),
@access_token.token,
@access_token.secret
)
puts "4. 访问受限资源"
@response=@access_token.get "/people/%40me?alt=json"
puts @response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment