Cunsumer
gem install oauth
大概:
require 'rubygems'
require 'oauth'
# your api key here
sina_api_key = " "
# your api key secret here
sina_api_key_secret = " "
@consumer = OAuth::Consumer.new(
sina_api_key,
sina_api_key_secret,
{
:site => "sina ",
}
#1 get request_token
@request_token = @consumer.get_request_token
#2 authorize & get oauth_verifer
puts "Copy this url to your browser to authorize, and get the oauth verifier code:"
puts @request_token.authorize_url
@oauth_verifier = " " # put the verifier code here
#3 get access_token
@access_token = @request_token.get_access_token(:oauth_verifier => @oauth_verifier)
#4 API Example: get current user info
puts @access_token.get "/account/verify_credentials.xml"
#API result :
XML
http://www.javaeye.com/topic/866280
配置参数
: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"
CLIENT
# 1. 获取Request Token
# 2. 用户确认授权
# 3. 换取Access Token
# 4. 访问受限资源
授权的链接如下,后面的红色部分,就是得到的request token
http://api.t.sina.com.cn/oauth/authorize?oauth_token=e54abbaff178411f7c1aeda9736376fc
打开授权链接,对应用完成授权,会自动跳转到指定的callback url,并将oauth_verifier一起返回。形如下面的形式:http://www.javaeye.com /?oauth_token=e54abbaff178411f7c1aeda9736376fc&oauth_verifier= 637479
Provider
rails + oauth_plugin + service Provider
rails new oauthServiceProvider
sudo gem install oauth
gemfile add gem 'oauth-plugin', '>=0.4.0.pre1'
bundle install
then install auth plugin:
cd vendor/plugins
git clone git://github.com/Satish/restful-authentication.git restful_authentication
cd restful_authentication
rm -rf .git
then go back to rails app home and type rails g authenticated user sessions
To create oauth_provider: rails g oauth_provider
问题1: uninitialized constant UsersController::AuthenticatedSystem
解决方案:
In your config/initializers directory create a file 'custom_requires.rb'
In this file put the following line:
require 'authenticated_system.rb'
Restart your server.