pythongoogle-cloud-platformgoogle-oauthgoogle-api-python-clientgoogle-cloud-shell

run_console oauth flow does't exist as the documentation suggests?


I try to run oauth flow code in python on a google-cloud shell.

enter image description here

But I get an error as the method "run_console" doesn't exist. How can it be?

enter image description here


Solution

  • run_console was deprecated don't use that method

    New clients will be unable to use InstalledAppFlow.run_console starting on Feb 28, 2022. All clients will be unable to use this method starting on Oct 3, 2022. Use InstalledAppFlow.run_local_server instead. For details on the OOB flow deprecation, see https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html?m=1#disallowed-oob

    use run_local_server for installed applications.

    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}')