def get_credentials(self, cred_file='client_secret.json'):
import os
from oauth2client.file import Storage
from oauth2client import client
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
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(cred_file, scope=['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'])
flow.user_agent = self.app_name
try:
credentials = tools.run_flow(flow, store, self.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
评论列表
文章目录