def get_credentials(client_secret_file, credentials_file, scopes, user_agent, args=None):
"""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.
"""
store = oauth2client.file.Storage(credentials_file)
credentials = store.get()
if not credentials or credentials.invalid:
flow = oauth2client.client.flow_from_clientsecrets(client_secret_file, scopes)
flow.user_agent = user_agent
if args:
credentials = oauth2client.tools.run_flow(flow, store, args)
else: # Needed only for compatibility with Python 2.6
credentials = oauth2client.tools.run(flow, store)
print('Storing credentials to ' + credentials_file)
return credentials
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/gmail-python-import.json
评论列表
文章目录