|
1 | 1 | import pickle |
2 | 2 | import os.path |
| 3 | +import glob |
3 | 4 | from typing import List |
4 | 5 |
|
5 | 6 | from googleapiclient import discovery |
@@ -32,10 +33,11 @@ def __init__( |
32 | 33 | Credentials with token and refresh token. |
33 | 34 | If specified, ``credentials_path``, ``token_path``, and ``save_token`` are ignored. |
34 | 35 | If not specified, credentials are retrieved from "token.pickle" file (specified in ``token_path`` or |
35 | | - default path) or with authentication flow using secret from "credentials.json" (specified in |
36 | | - ``credentials_path`` or default path) |
| 36 | + default path) or with authentication flow using secret from "credentials.json" ("client_secret_*.json") |
| 37 | + (specified in ``credentials_path`` or default path) |
37 | 38 | :param credentials_path: |
38 | | - Path to "credentials.json" file. Default: ~/.credentials |
| 39 | + Path to "credentials.json" ("client_secret_*.json") file. |
| 40 | + Default: ~/.credentials/credentials.json or ~/.credentials/client_secret*.json |
39 | 41 | :param token_path: |
40 | 42 | Existing path to load the token from, or path to save the token after initial authentication flow. |
41 | 43 | Default: "token.pickle" in the same directory as the credentials_path |
@@ -109,12 +111,28 @@ def _get_credentials( |
109 | 111 |
|
110 | 112 | @staticmethod |
111 | 113 | def _get_default_credentials_path() -> str: |
112 | | - """Checks if ".credentials" folder in home directory exists. If not, creates it. |
113 | | - :return: expanded path to .credentials folder |
| 114 | + """Checks if `.credentials` folder in home directory exists and contains `credentials.json` or |
| 115 | + `client_secret*.json` file. |
| 116 | +
|
| 117 | + :raises ValueError: if `.credentials` folder does not exist, none of `credentials.json` or `client_secret*.json` |
| 118 | + files do not exist, or there are multiple `client_secret*.json` files. |
| 119 | + :return: expanded path to `credentials.json` or `client_secret*.json` file |
114 | 120 | """ |
115 | 121 | home_dir = os.path.expanduser('~') |
116 | 122 | credential_dir = os.path.join(home_dir, '.credentials') |
117 | 123 | if not os.path.exists(credential_dir): |
118 | | - os.makedirs(credential_dir) |
| 124 | + raise FileNotFoundError(f'Default credentials directory "{credential_dir}" does not exist.') |
119 | 125 | credential_path = os.path.join(credential_dir, 'credentials.json') |
120 | | - return credential_path |
| 126 | + if os.path.exists(credential_path): |
| 127 | + return credential_path |
| 128 | + else: |
| 129 | + credentials_files = glob.glob(credential_dir + '/client_secret*.json') |
| 130 | + if len(credentials_files) > 1: |
| 131 | + raise ValueError(f"Multiple credential files found in {credential_dir}.\n" |
| 132 | + f"Try specifying the credentials file, e.x.:\n" |
| 133 | + f"GoogleCalendar(credentials_path='{credentials_files[0]}')") |
| 134 | + elif not credentials_files: |
| 135 | + raise FileNotFoundError(f'Credentials file (credentials.json or client_secret*.json)' |
| 136 | + f'not found in the default path: "{credential_dir}".') |
| 137 | + else: |
| 138 | + return credentials_files[0] |
0 commit comments