def get_credentials(self, cred_path):
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
if not cred_path:
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
try:
os.system("sudo mkdir {}".format(credential_dir))
except:
os.umask(0)
os.makedirs(credential_dir, mode=0o777)
else:
credential_dir = cred_path
credential_path = os.path.join(credential_dir,
'python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(self.secret_file, scope=['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'])
flow.user_agent = self.app_name
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser], conflict_handler='resolve').parse_args()
except ImportError:
flags = None
try:
credentials = tools.run_flow(flow, store, flags)
except: # Needed only for compatibility with Python 2.6
try:
credentials = tools.run_flow(flow, store)
except:
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
评论列表
文章目录