run_console oauth 流程不存在,如文档所示?

问题描述 投票:0回答:1

我尝试在 google-cloud shell 上用 python 运行 oauth 流代码。

但是我收到错误,因为方法“run_console”不存在。怎么可能?

python google-cloud-platform google-oauth google-api-python-client google-cloud-shell
1个回答
0
投票

run_console 已弃用,请勿使用该方法

从 2022 年 2 月 28 日开始,新客户将无法使用

InstalledAppFlow.run_console
。从 2022 年 10 月 3 日开始,所有客户将无法使用此方法。请改用
InstalledAppFlow.run_local_server
。有关弃用 OOB 流的详细信息,请参阅 https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html?m=1#disallowed-oob

使用 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}')
© www.soinside.com 2019 - 2024. All rights reserved.