Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created December 10, 2010 10:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marshluca/736056 to your computer and use it in GitHub Desktop.
Save marshluca/736056 to your computer and use it in GitHub Desktop.
豆瓣OAuth认证
# Douban OAuth认证包括以下四步内容
# 1. 获取Request Token
# 2. 用户确认授权
# 3. 换取Access Token
# 4. 访问受限资源
require "rubygems"
gem 'oauth','0.4.3'
require 'oauth'
API_KEY = "0d205692ee17468b276103b8ca45d3a1"
API_KEY_SECRET = "eca5d4f377ac66c4"
SITE = "http://www.douban.com"
# 1. 获取Request Token
options = {
:site=> SITE,
: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://www.5yi.com"
}
@consumer = OAuth::Consumer.new(API_KEY, API_KEY_SECRET, options)
@request_token=@consumer.get_request_token
# 2. 用户确认授权
url = @request_token.authorize_url
puts "请将下面url粘贴到浏览器中,并同意授权,同意后按任意键继续:\n#{url}"
sleep(15)
# 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(@consumer,@access_token.token,@access_token.secret)
# 4. 访问受限资源
response = @access_token.get "http://api.douban.com/people/%40me"
puts response.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment