Created
August 21, 2012 13:08
-
-
Save liluo/3415272 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1, 获取 access token | |
get '/auth/douban/callback' do | |
auth = env['omniauth.auth'] | |
credentials = auth['credentials'] | |
end | |
在这里可以拿到 credentials, 大致格式: | |
{"token"=>"xxxxxxxxxxxxxxxxx", "expires_at"=>1345640145, "expires"=>true} | |
token = credentials['token'] | |
2, 使用 gem oauth2 | |
require 'oauth2' | |
KEY = 'your key' | |
SECRET = 'your secret' | |
token = 'xxxxxx' # 第1步中获取的 token | |
client = OAuth2::Client.new(KEY, SECRET, { site: 'https://api.douban.com', | |
authorize_url: 'https://www.douban.com/service/auth2/auth', | |
token_url: 'https://www.douban.com/service/auth2/token'}) | |
access_token = OAuth2::AccessToken.new(client, token) | |
3, 然后就可以拿 access_token 进行操作了 | |
puts access_token.get('/v2/people/~me').parsed | |
puts access_token.get('/doumail/inbox').parsed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment