Skip to content

Commit

Permalink
Merge pull request #4 from octoenergy/add_token_generation_cli
Browse files Browse the repository at this point in the history
Add token generation cli
  • Loading branch information
j0nnyr0berts authored Feb 7, 2022
2 parents dcaed24 + ceaba30 commit 4a761ef
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.1]
## [1.0.3] - 2022-02-07
### Added
- Add click command for generating gdrive token
- Bump tentaclio install requirement >= 1.0.3

## [0.0.1] - 2021-11-26
### Added
- Google drive client.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ Google drive support is _experimental_ and should be used at your own risk. Also

1. Get the credentials.
First we need a credentials file in order to be able to generate tokens. The easiest way to do this is by going to [this example](https://developers.google.com/drive/api/v3/quickstart/python),
click on enable drive api. Give the project a name of your choosing (eg `tentaclio`), set the OAuth
client selector to "Desktop app", and download the generated JSON file.
click on enable drive api. Give the project a name of your choosing (eg `tentaclio`). Click on `APIs and services` -> `Credentials` -> `Create credentials` -> Create OAuth client ID`, select `Desktop app` and `Download JSON`

2. Generate token file

```
pipenv install tentaclio && \
pipenv run python -m tentaclio google-token generate --credentials-file ~/Downloads/credentials.json
pipenv run python -m tentaclio_gdrive google-token generate --credentials-file ~/Downloads/credentials.json
```
This will open a browser with a google auth page, log in and accept the authorisation request.
The token file has been saved in a default location '~/.tentaclio_google_drive.json'. You can also configure this via the env variable `TENTACLIO__GOOGLE_DRIVE_TOKEN_FILE`

3. Get rid of credentials.json
The `credentials.json` file is not longer need, feel free to delete it.
The `credentials.json` file is no longer need, feel free to delete it.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

VERSION = "0.0.1"
VERSION = "0.0.2"

REPO_ROOT = pathlib.Path(__file__).parent

Expand All @@ -34,7 +34,7 @@ def run(self):


install_requires = [
"tentaclio",
"tentaclio>=1.0.2",
"google-api-python-client",
"google-auth-httplib2",
"google-auth-oauthlib",
Expand Down
47 changes: 45 additions & 2 deletions src/tentaclio_gdrive/__main__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,51 @@
# Helper cli to encode urls.
import click
from google_auth_oauthlib.flow import InstalledAppFlow

from tentaclio_gdrive.clients import google_drive_client as gd

from tentaclio.__main__ import main

SCOPES = ["https://www.googleapis.com/auth/drive"]


@click.group()
def main():
"""Run tentaclio helper commands."""
...


@main.group()
def google_token():
"""Manage google tokens."""
...


@google_token.command()
@click.option(
"--credentials-file",
required=True,
help=(
"Settings file to generate the token from, can be obtained from "
"https://developers.google.com/drive/api/v3/quickstart/python "
"by clicking enable drive api."
),
)
@click.option(
"--output-file",
default=gd.DEFAULT_TOKEN_FILE,
help=(
"Output token for google api requests. if set to other value than the default,"
" TENTACLIO_GOOGLE_DRIVE_TOKEN_FILE variable needs to be set accordingly."
),
)
def generate(credentials_file, output_file):
"""Generate token file from the settings.json file."""
flow = InstalledAppFlow.from_client_secrets_file(credentials_file, SCOPES)
creds = flow.run_local_server(port=0)
with open(output_file, "w") as f:
f.write(creds.to_json())
print(f"Token file saved to {output_file}")


if __name__ == "__main__":
main(prog_name="python -m tentaclio-gdrive")
main(prog_name="python -m tentaclio_gdrive")

0 comments on commit 4a761ef

Please sign in to comment.