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