blob: be72497b99f24b84f904a98ec8022fbaa34bfdab [file] [log] [blame]
Joe Gregorio9b57dd32011-02-17 15:53:50 -05001# Copyright 2010 Google Inc. All Rights Reserved.
2
3"""Utilities for OAuth.
4
5Utilities for making it easier to work with OAuth 1.0
6credentials.
7"""
8
9__author__ = 'jcgregorio@google.com (Joe Gregorio)'
10
11import pickle
12
Joe Gregorioa0a52e42011-02-17 17:13:26 -050013from apiclient.oauth import Storage as BaseStorage
Joe Gregorio9b57dd32011-02-17 15:53:50 -050014
Joe Gregorioa0a52e42011-02-17 17:13:26 -050015
16class Storage(BaseStorage):
Joe Gregorio9b57dd32011-02-17 15:53:50 -050017 """Store and retrieve a single credential to and from a file."""
18
19 def __init__(self, filename):
20 self._filename = filename
21
22 def get(self):
23 """Retrieve Credential from file.
24
25 Returns:
26 apiclient.oauth.Credentials
27 """
28 try:
29 f = open(self._filename, 'r')
30 credentials = pickle.loads(f.read())
31 f.close()
32 except:
33 credentials = None
Joe Gregorioa0a52e42011-02-17 17:13:26 -050034 credentials.set_store(self.put)
Joe Gregorio9b57dd32011-02-17 15:53:50 -050035
36 return credentials
37
38 def put(self, credentials):
39 """Write a pickled Credentials to file.
40
41 Args:
42 credentials: Credentials, the credentials to store.
43 """
44 f = open(self._filename, 'w')
45 f.write(pickle.dumps(credentials))
46 f.close()