예제 #1
0
    def run_flow(self):
        with open(self.client_secret, 'r') as file:
            client_config = json.load(file)

        flow = InstalledAppFlow.from_client_config(client_config, self.scopes)
        if self.server_port is None:
            raise ValueError(
                "server_port not specified. Usual values are: 8080, 8000, 8888"
            )
        num_fails = 0
        success = False
        while not success:
            try:
                self.credentials = InstalledAppFlow.run_local_server(
                    flow, port=self.server_port)
                success = True
                LOG.info(
                    f"Failed to start the local server {num_fails} times.")
            except OSError as err:
                self.server_port += 1
                num_fails += 1
                if num_fails > 100:
                    LOG.error(
                        "Failed to start the local server more than 100 times."
                    )
                    raise err

        # save obtained credentials for faster authorization next time
        self.save_credentials()