Fixed handling of expired access tokens.
authorBob Aman <bobaman@google.com>
Sat, 21 May 2011 00:05:05 +0000 (00:05 +0000)
committerBob Aman <bobaman@google.com>
Sat, 21 May 2011 00:05:05 +0000 (00:05 +0000)
git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@163 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef

examples/buzz.rb

index ad94e9ba0d2675516e00b53e0ca47da91ad03c10..1f0a5da9ddddbdef111342c91cefcfb41b5e69ac 100644 (file)
@@ -14,19 +14,22 @@ class TokenPair
   property :id, Serial
   property :refresh_token, String
   property :access_token, String
-  property :expires_at, Integer
+  property :expires_in, Integer
+  property :issued_at, Integer
 
   def update_token!(object)
     self.refresh_token = object.refresh_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
 
   def to_hash
     return {
       :refresh_token => refresh_token,
       :access_token => access_token,
-      :expires_at => expires_at
+      :expires_in => expires_in,
+      :issued_at => Time.at(issued_at)
     }
   end
 end
@@ -44,7 +47,9 @@ before do
     token_pair = TokenPair.get(session[:token_id])
     @client.authorization.update_token!(token_pair.to_hash)
   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')
   unless @client.authorization.access_token || request.path_info =~ /^\/oauth2/
     redirect to('/oauth2authorize')