run_console 已弃用,请勿使用该方法
从 2022 年 2 月 28 日开始,新客户将无法使用
。从 2022 年 10 月 3 日开始,所有客户将无法使用此方法。请改用InstalledAppFlow.run_console
。有关弃用 OOB 流的详细信息,请参阅 https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html?m=1#disallowed-oobInstalledAppFlow.run_local_server
使用 run_local_server 来安装应用程序。
def build_service(credentials, scope, user_token):
creds = None
if os.path.exists(user_token):
creds = Credentials.from_authorized_user_file(user_token, scope)
# If there are no (valid) user credentials available, prompt the user to 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, scope)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(user_token, 'w') as token:
token.write(creds.to_json())
try:
return build('drive', 'v3', credentials=creds)
except HttpError as error:
# TODO(developer) - any errors returned.
print(f'An error occurred: {error}')