Esempio n. 1
0
    def client(self) -> Client:
        from gspread.auth import (
            InstalledAppFlow,
            load_credentials,
            store_credentials,
            Client,
        )

        authorized_user_path = pathlib.Path(self.authorized_user_filename).expanduser()
        credential_path = pathlib.Path(self.credential_filename).expanduser()
        if not authorized_user_path.parent.exists():
            authorized_user_path.parent.mkdir(parents=True)
        if not credential_path.parent.exists():
            credential_path.parent.mkdir(parents=True)

        creds = load_credentials(filename=str(authorized_user_path))

        if not creds:
            try:
                flow = InstalledAppFlow.from_client_secrets_file(
                    str(credential_path), self.scopes
                )
            except FileNotFoundError as e:
                raise exceptions.CredentialsFileIsNotFound(e.filename)

            creds = flow.run_local_server(port=self.port)
            store_credentials(creds, filename=str(authorized_user_path))

        client = Client(auth=creds)
        return client
Esempio n. 2
0
    async def run(
        self,
        credential_filename: str = "~/.config/sheetconf/credentials.json",
        authorized_user_filename: str = ".auth.out",
        scopes: t.Optional[t.List[str]] = DEFAULT_SCOPES,
    ):
        prompt = self.prompt
        get_input = self.input.get_input

        authorized_user_path = pathlib.Path(
            authorized_user_filename).expanduser()
        credential_path = pathlib.Path(credential_filename).expanduser()
        if not authorized_user_path.parent.exists():
            authorized_user_path.parent.mkdir(parents=True)
        if not credential_path.parent.exists():
            credential_path.parent.mkdir(parents=True)

        creds = load_credentials(filename=str(authorized_user_path))

        if creds:
            return creds

        try:
            flow = InstalledAppFlow.from_client_secrets_file(
                str(credential_path), scopes)
        except FileNotFoundError:
            raise  # xxx

        kwargs = {}
        kwargs.setdefault("prompt", "consent")

        flow.redirect_uri = flow._OOB_REDIRECT_URI
        auth_url, _ = flow.authorization_url(**kwargs)
        await self.prompt.message(
            flow._DEFAULT_AUTH_PROMPT_MESSAGE.format(url=auth_url))
        await self.prompt.prompt(flow._DEFAULT_AUTH_CODE_MESSAGE)
        life = 3

        result = None
        for i in range(life):
            code = await get_input()
            try:
                flow.fetch_token(code=code)
                result = flow.credentials
                break
            except Exception as e:
                await prompt.message(f"invalid text {e}")

        if result is not None:
            await prompt.message(f"ok: {result}", private=False)
            store_credentials(creds, filename=str(authorized_user_path))
        else:
            await prompt.message("giveup", private=False)
Esempio n. 3
0
def oauth(scopes=SCOPES, port=0):
    from gspread.auth import (
        InstalledAppFlow,
        load_credentials,
        store_credentials,
        Client,
    )

    credential_filename = os.path.expanduser("~/.config/sheetconf/credentials.json")
    authorized_user_filename = os.path.expanduser(
        "~/.config/sheetconf/authorized_user.json"
    )
    creds = load_credentials(filename=authorized_user_filename)

    if not creds:
        flow = InstalledAppFlow.from_client_secrets_file(credential_filename, scopes)
        creds = flow.run_local_server(port=port)
        store_credentials(creds, filename=authorized_user_filename)

    client = Client(auth=creds)
    return client
Esempio n. 4
0
def get_credentials(
    credential_filename: str = "~/.config/sheetconf/credentials.json",
    authorized_user_filename: str = ".auth.out",
    scopes: t.Optional[t.List[str]] = DEFAULT_SCOPES,
):
    authorized_user_path = pathlib.Path(authorized_user_filename).expanduser()
    credential_path = pathlib.Path(credential_filename).expanduser()
    if not authorized_user_path.parent.exists():
        authorized_user_path.parent.mkdir(parents=True)
    if not credential_path.parent.exists():
        credential_path.parent.mkdir(parents=True)

    creds = load_credentials(filename=str(authorized_user_path))

    if not creds:
        try:
            flow = InstalledAppFlow.from_client_secrets_file(
                str(credential_path), scopes)
        except FileNotFoundError:
            raise  # xxx

        creds = flow.run_console()
        store_credentials(creds, filename=str(authorized_user_path))
    return creds