-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_auth_creds.py
More file actions
executable file
·70 lines (57 loc) · 1.99 KB
/
get_auth_creds.py
File metadata and controls
executable file
·70 lines (57 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.14"
# dependencies = [
# "google-auth",
# "google-auth-httplib2",
# "google-auth-oauthlib",
# ]
# ///
"""-----------------------------------------------------------
get_auth_creds.py -- retrieve several fields from OAuth 2.0
credentials file and output them to standard out, for use in
configuration scripts, such as OfflineIMAP.
For example:
get_auth_creds.py my-credentials.json client_id
get_auth_creds.py my-credentials.json client_secret
get_auth_creds.py my-credentials.json refresh_token
If you just need the client token (e.g., to login), see get_token.py.
"""
import sys
import argparse
from pathlib import Path
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
def parse_args() -> argparse.Namespace:
"""Parse command-line arguments"""
parser = argparse.ArgumentParser(
prog="get_auth_creds.py",
description="Return various fields from an OAuth 2.0 credentials file.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("credentials")
parser.add_argument(
"field",
choices=["client_id", "client_secret", "refresh_token"],
help="The field to query.",
)
return parser.parse_args()
def get_field(credentials: Path, field: str) -> str:
"""Return the given field from the given creds file"""
if not credentials.exists():
sys.exit(f"{credentials} file not found.")
creds = Credentials.from_authorized_user_file(credentials)
if field == "client_id":
return creds.client_id
elif field == "client_secret":
return creds.client_secret
elif field == "refresh_token":
return creds.refresh_token
else:
raise ValueError(f"Unknown field: {field}")
if __name__ == "__main__":
args = parse_args()
creds_file = args.credentials
field = args.field
result = get_field(Path(creds_file), field)
print(f"{result}")