-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.rb
82 lines (69 loc) · 1.85 KB
/
example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# encoding: utf-8
require 'rubygems'
require 'bundler'
Bundler.require
enable :sessions
WeiboOAuth2::Config.api_key = ENV['KEY']
WeiboOAuth2::Config.api_secret = ENV['SECRET']
WeiboOAuth2::Config.redirect_uri = ENV['REDIR_URI']
get '/' do
client = WeiboOAuth2::Client.new
if session[:access_token] && !client.authorized?
token = client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
p "*" * 80 + "validated"
p token.inspect
p token.validated?
unless token.validated?
reset_session
redirect '/connect'
return
end
end
if session[:uid]
@user = client.users.show_by_uid(session[:uid])
@statuses = client.statuses
end
haml :index
end
get '/connect' do
client = WeiboOAuth2::Client.new
redirect client.authorize_url
end
get '/callback' do
client = WeiboOAuth2::Client.new
access_token = client.auth_code.get_token(params[:code].to_s)
session[:uid] = access_token.params["uid"]
session[:access_token] = access_token.token
session[:expires_at] = access_token.expires_at
p "*" * 80 + "callback"
p access_token.inspect
@user = client.users.show_by_uid(session[:uid].to_i)
redirect '/'
end
get '/logout' do
reset_session
redirect '/'
end
get '/screen.css' do
content_type 'text/css'
sass :screen
end
post '/update' do
client = WeiboOAuth2::Client.new
client.get_token_from_hash({:access_token => session[:access_token], :expires_at => session[:expires_at]})
statuses = client.statuses
unless params[:file] && (pic = params[:file].delete(:tempfile))
statuses.update(params[:status])
else
status = params[:status] || '图片'
statuses.upload(status, pic, params[:file])
end
redirect '/'
end
helpers do
def reset_session
session[:uid] = nil
session[:access_token] = nil
session[:expires_at] = nil
end
end