本文整理汇总了Python中google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file方法的典型用法代码示例。如果您正苦于以下问题:Python InstalledAppFlow.from_client_secrets_file方法的具体用法?Python InstalledAppFlow.from_client_secrets_file怎么用?Python InstalledAppFlow.from_client_secrets_file使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google_auth_oauthlib.flow.InstalledAppFlow
的用法示例。
在下文中一共展示了InstalledAppFlow.from_client_secrets_file方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getCreds
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def getCreds():
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
creds = None
SCOPES = 'https://www.googleapis.com/auth/drive'
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return creds
示例2: from_client_secrets_file
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def from_client_secrets_file(cls, client_secrets_file, scopes, **kwargs):
"""Creates a :class:`Flow` instance from a Google client secrets file.
Args:
client_secrets_file (str): The path to the client secrets .json
file.
scopes (Sequence[str]): The list of scopes to request during the
flow.
kwargs: Any additional parameters passed to
:class:`requests_oauthlib.OAuth2Session`
Returns:
Flow: The constructed Flow instance.
"""
with open(client_secrets_file, "r") as json_file:
client_config = json.load(json_file)
return cls.from_client_config(client_config, scopes=scopes, **kwargs)
示例3: __init__
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def __init__(self, address):
"""
Initialise a GMail notifier object. Prompt the user to authenticate
and provide mailing permissions if required.
"""
self.address = address
self.creds = None
# if there's an access token from previous authentication, load it
if os.path.exists(ACCESS_TOKEN_PATH):
with open(ACCESS_TOKEN_PATH, 'rb') as tokenfile:
self.creds = pickle.load(tokenfile)
# if the credentials are invalid or non-existent, prompt to authenticate
if not self.creds or not self.creds.valid:
if self.creds and self.creds.expired and self.creds.refresh_token:
self.creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
CLIENT_ID_PATH, SCOPES)
self.creds = flow.run_local_server()
# save the credentials for the next run
with open(ACCESS_TOKEN_PATH, 'wb') as tokenfile:
pickle.dump(self.creds, tokenfile)
self.service = build('gmail', 'v1', credentials=self.creds)
示例4: get_creds
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def get_creds(credentials,token,scopes=['https://www.googleapis.com/auth/drive']):
creds = None
if path.exists(token):
with open(token,'r') as t:
creds = json_to_cred(t)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(credentials,scopes)
creds = flow.run_local_server(port=0)
with open(token,'w') as t:
json.dump(cred_to_json(creds),t,indent=2)
return creds
示例5: authorize
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def authorize(self):
# Get credentials
credentials = None
if not USE_SERVICE_ACCOUNTS:
if os.path.exists(self.__G_DRIVE_TOKEN_FILE):
with open(self.__G_DRIVE_TOKEN_FILE, 'rb') as f:
credentials = pickle.load(f)
if credentials is None or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', self.__OAUTH_SCOPE)
LOGGER.info(flow)
credentials = flow.run_console(port=0)
# Save the credentials for the next run
with open(self.__G_DRIVE_TOKEN_FILE, 'wb') as token:
pickle.dump(credentials, token)
else:
LOGGER.info(f"Authorizing with {SERVICE_ACCOUNT_INDEX}.json service account")
credentials = service_account.Credentials.from_service_account_file(
f'accounts/{SERVICE_ACCOUNT_INDEX}.json',
scopes=self.__OAUTH_SCOPE)
return build('drive', 'v3', credentials=credentials, cache_discovery=False)
示例6: set_google_credentials
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def set_google_credentials(authorized_user_file,
key_file_location,
scope):
try:
credentials = Credentials.from_authorized_user_file(authorized_user_file, scopes=[scope])
except FileNotFoundError:
flow = InstalledAppFlow.from_client_secrets_file(key_file_location, [scope])
credentials = flow.run_console()
os.makedirs(os.path.expanduser('~/.cache/'), exist_ok=True)
with open(authorized_user_file, 'w') as file:
json.dump({
'client_id': credentials._client_id,
'client_secret': credentials._client_secret,
'refresh_token': credentials._refresh_token
}, file)
os.chmod(authorized_user_file, 0o600)
return credentials
示例7: init_email_sending
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def init_email_sending():
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
creds = None
try:
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_console()
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('gmail', 'v1', credentials=creds)
return True
except Exception as e:
print(e)
return False
示例8: get_authenticated_service
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def get_authenticated_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_console()
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)
示例9: __init__
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def __init__(self, oauth_client_secrets_path):
flow = InstalledAppFlow.from_client_secrets_file(
oauth_client_secrets_path,
scopes=['https://www.googleapis.com/auth/siteverification'])
credentials = flow.run_console()
http = google_auth_httplib2.AuthorizedHttp(
credentials, http=httplib2.Http())
self.api = discovery.build('siteVerification', 'v1', http=http)
示例10: set_session
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def set_session(self):
# Try to load credentials from an auth file.
# If it doesn't exist or is not valid then catch the
# exception and reauthenticate.
try:
creds = Credentials.from_authorized_user_file(self.auth_file, self.scopes)
except:
try:
flow = InstalledAppFlow.from_client_secrets_file(self.secrets_file, self.scopes)
creds = flow.run_local_server()
cred_dict = {
'token': creds.token,
'refresh_token': creds.refresh_token,
'id_token': creds.id_token,
'scopes': creds.scopes,
'token_uri': creds.token_uri,
'client_id': creds.client_id,
'client_secret': creds.client_secret
}
# Store the returned authentication tokens to the auth_file.
with open(self.auth_file, 'w') as f:
f.write(json.dumps(cred_dict))
except:
return
self.session = AuthorizedSession(creds)
self.session.headers["Content-type"] = "application/octet-stream"
self.session.headers["X-Goog-Upload-Protocol"] = "raw"
示例11: matrix_start
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def matrix_start(self, bot):
super().matrix_start(bot)
self.bot = bot
creds = None
if not os.path.exists(self.credentials_file) or os.path.getsize(self.credentials_file) == 0:
return # No-op if not set up
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
self.logger.info('Loaded existing pickle file')
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
self.logger.warn('No credentials or credentials not valid!')
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(self.credentials_file, self.SCOPES)
# urn:ietf:wg:oauth:2.0:oob
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
self.logger.info('Pickle saved')
self.service = build('calendar', 'v3', credentials=creds)
try:
calendar_list = self.service.calendarList().list().execute()['items']
self.logger.info(f'Google calendar set up successfully with access to {len(calendar_list)} calendars:\n')
for calendar in calendar_list:
self.logger.info(f"{calendar['summary']} - + {calendar['id']}")
except Exception:
self.logger.error('Getting calendar list failed!')
示例12: main
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def main(client_secrets_path, scopes):
flow = InstalledAppFlow.from_client_secrets_file(
client_secrets_path, scopes=scopes)
flow.run_console()
print('Access token: %s' % flow.credentials.token)
print('Refresh token: %s' % flow.credentials.refresh_token)
示例13: main
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def main(client_secrets_path, scopes):
flow = InstalledAppFlow.from_client_secrets_file(
client_secrets_path, scopes=scopes)
flow.run_local_server()
print('Access token: %s' % flow.credentials.token)
print('Refresh token: %s' % flow.credentials.refresh_token)
示例14: authenticate
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def authenticate():
print('Authenticating')
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
print('Refreshing credentials')
creds.refresh(Request())
else:
print('Need to allow access')
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
return service
示例15: get_authenticated_service
# 需要导入模块: from google_auth_oauthlib.flow import InstalledAppFlow [as 别名]
# 或者: from google_auth_oauthlib.flow.InstalledAppFlow import from_client_secrets_file [as 别名]
def get_authenticated_service():
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_console()
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)