ruby+OAuth

 

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值