Skip to content

Commit

Permalink
remove service account and use credentials.json
Browse files Browse the repository at this point in the history
  • Loading branch information
sdaoudi committed Sep 17, 2023
1 parent bd8712d commit 1e3d66f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ In your `configuration.yaml`, include:

```yaml
gdrive_uploader:
secret_file_path: "/config/secrets/service-account-secrets.json"
secret_file_path: "/config/secrets/credentials.json"
```
The `secret_file_path` is the path to your service account keys file generated on the Google console.

If you don't have a service account yet, [click here](./SERVICE_ACCOUNT.md) to access the detailed documentation on creating a service account.
The `credentials.json` is the path to your credetnials.json file generated with your client_id and client_secret.

## Usage

Expand Down
12 changes: 6 additions & 6 deletions custom_components/gdrive_uploader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

from .api import GDriveApi
from .const import (ATTR_DIR_NAME, ATTR_PARENT_ID, ATTR_TARGET_DIR_NAME, ATTR_UPLOAD_FILE_PATH,
CONF_SECRET_FILE_PATH, DOMAIN, UPLOAD_COMPLETED_EVENT, UPLOAD_FAILED_EVENT)
CREDENTIALS_FILE_PATH, DOMAIN, UPLOAD_COMPLETED_EVENT, UPLOAD_FAILED_EVENT)

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_SECRET_FILE_PATH): cv.isfile,
vol.Required(CREDENTIALS_FILE_PATH): cv.isfile,
}
)
},
Expand All @@ -23,11 +23,11 @@

def setup(hass, config):
def handle_upload(call):
secret_file_path = config.get(DOMAIN, {}).get(CONF_SECRET_FILE_PATH)
credentials_file_path = config.get(DOMAIN, {}).get(CREDENTIALS_FILE_PATH)
upload_file_path = call.data.get(ATTR_UPLOAD_FILE_PATH)
parent_id = call.data.get(ATTR_PARENT_ID)
target_dir_name = call.data.get(ATTR_TARGET_DIR_NAME, "")
gdrive = GDriveApi(secret_file_path)
gdrive = GDriveApi(credentials_file_path)
try:
file = gdrive.upload_file(upload_file_path, parent_id, target_dir_name)
hass.bus.fire(
Expand All @@ -42,10 +42,10 @@ def handle_upload(call):
)

def handle_delete(call):
secret_file_path = config.get(DOMAIN, {}).get(CONF_SECRET_FILE_PATH)
credentials_file_path = config.get(DOMAIN, {}).get(CREDENTIALS_FILE_PATH)
parent_id = call.data.get(ATTR_PARENT_ID)
dir_name = call.data.get(ATTR_DIR_NAME)
gdrive = GDriveApi(secret_file_path)
gdrive = GDriveApi(credentials_file_path)
delete_lock = threading.Lock()

def do_delete():
Expand Down
23 changes: 14 additions & 9 deletions custom_components/gdrive_uploader/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
_LOGGER = logging.getLogger(__name__)

class GDriveApi:
def __init__(self, client_secret_file_path: str):
settings = {
"client_config_backend": "service",
"service_config": {
"client_json_file_path": client_secret_file_path,
}
}
gauth = GoogleAuth(settings=settings)
gauth.ServiceAuth()
def __init__(self, credentials_file_path: str):
gauth = GoogleAuth()

# Try to load saved client credentials
gauth.LoadCredentialsFile(credentials_file_path)
if gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile(credentials_file_path)

self.drive = GoogleDrive(gauth)

def _create_folder(self, parent_folder_id, subfolder_name):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/gdrive_uploader/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ATTR_TARGET_DIR_NAME = "target_dir_name"
ATTR_UPLOAD_FILE_PATH = "upload_file_path"

CONF_SECRET_FILE_PATH = "secret_file_path"
CREDENTIALS_FILE_PATH = "credentials_file_path"

DOMAIN = "gdrive_uploader"
UPLOAD_COMPLETED_EVENT = "upload_completed"
Expand Down

0 comments on commit 1e3d66f

Please sign in to comment.