Fixed handling of expired access tokens.

git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@163 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
Bob Aman 2011-05-21 00:05:05 +00:00
parent af87738e06
commit a656c13862
1 changed files with 9 additions and 4 deletions

View File

@ -14,19 +14,22 @@ class TokenPair
property :id, Serial property :id, Serial
property :refresh_token, String property :refresh_token, String
property :access_token, String property :access_token, String
property :expires_at, Integer property :expires_in, Integer
property :issued_at, Integer
def update_token!(object) def update_token!(object)
self.refresh_token = object.refresh_token self.refresh_token = object.refresh_token
self.access_token = object.access_token self.access_token = object.access_token
self.expires_at = object.expires_at self.expires_in = object.expires_in
self.issued_at = object.issued_at
end end
def to_hash def to_hash
return { return {
:refresh_token => refresh_token, :refresh_token => refresh_token,
:access_token => access_token, :access_token => access_token,
:expires_at => expires_at :expires_in => expires_in,
:issued_at => Time.at(issued_at)
} }
end end
end end
@ -44,7 +47,9 @@ before do
token_pair = TokenPair.get(session[:token_id]) token_pair = TokenPair.get(session[:token_id])
@client.authorization.update_token!(token_pair.to_hash) @client.authorization.update_token!(token_pair.to_hash)
end end
@client.authorization.fetch_access_token! if @client.authorization.expired? if @client.authorization.refresh_token && @client.authorization.expired?
@client.authorization.fetch_access_token!
end
@buzz = @client.discovered_api('buzz') @buzz = @client.discovered_api('buzz')
unless @client.authorization.access_token || request.path_info =~ /^\/oauth2/ unless @client.authorization.access_token || request.path_info =~ /^\/oauth2/
redirect to('/oauth2authorize') redirect to('/oauth2authorize')