gclientid 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gclientid-0.1.0/CHANGELOG.md +12 -0
- gclientid-0.1.0/LICENSE +202 -0
- gclientid-0.1.0/MANIFEST.in +3 -0
- gclientid-0.1.0/PKG-INFO +121 -0
- gclientid-0.1.0/README.md +101 -0
- gclientid-0.1.0/gclientid/__init__.py +4 -0
- gclientid-0.1.0/gclientid/oauth.py +245 -0
- gclientid-0.1.0/gclientid/projects.py +50 -0
- gclientid-0.1.0/gclientid.egg-info/PKG-INFO +121 -0
- gclientid-0.1.0/gclientid.egg-info/SOURCES.txt +13 -0
- gclientid-0.1.0/gclientid.egg-info/dependency_links.txt +1 -0
- gclientid-0.1.0/gclientid.egg-info/requires.txt +7 -0
- gclientid-0.1.0/gclientid.egg-info/top_level.txt +1 -0
- gclientid-0.1.0/pyproject.toml +34 -0
- gclientid-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!-- do not remove -->
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### New Features
|
|
6
|
+
|
|
7
|
+
- Require fastcdp 0.0.8 ([#6](https://github.com/AnswerDotAI/gclientid/pull/6)), thanks to [@jph00](https://github.com/jph00)
|
|
8
|
+
- Use normal Chrome by default ([#5](https://github.com/AnswerDotAI/gclientid/pull/5)), thanks to [@jph00](https://github.com/jph00)
|
|
9
|
+
- Create local Gmail OAuth credential files ([#4](https://github.com/AnswerDotAI/gclientid/pull/4)), thanks to [@jph00](https://github.com/jph00)
|
|
10
|
+
- Document personal Gmail OAuth setup ([#3](https://github.com/AnswerDotAI/gclientid/pull/3)), thanks to [@jph00](https://github.com/jph00)
|
|
11
|
+
- Publish the privacy policy with GitHub Pages ([#2](https://github.com/AnswerDotAI/gclientid/pull/2)), thanks to [@jph00](https://github.com/jph00)
|
|
12
|
+
- Add Google Cloud project lifecycle automation ([#1](https://github.com/AnswerDotAI/gclientid/pull/1)), thanks to [@jph00](https://github.com/jph00)
|
gclientid-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
gclientid-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gclientid
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Create Google OAuth desktop client IDs locally
|
|
5
|
+
Author-email: Jeremy Howard <github@jhoward.fastmail.fm>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/AnswerDotAI/gclientid
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: fastcdp>=0.0.8
|
|
14
|
+
Requires-Dist: httpx
|
|
15
|
+
Provides-Extra: dev
|
|
16
|
+
Requires-Dist: fastship; extra == "dev"
|
|
17
|
+
Requires-Dist: build; extra == "dev"
|
|
18
|
+
Requires-Dist: twine; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# gclientid
|
|
22
|
+
|
|
23
|
+
Create Google OAuth desktop client IDs locally, without requiring `gcloud`.
|
|
24
|
+
|
|
25
|
+
`gclientid` uses an existing signed-in [fastcdp](https://github.com/AnswerDotAI/fastcdp) Chrome session. Enable **Allow remote debugging** in `chrome://inspect/#remote-debugging`, then create a globally unique project ID, provision its Gmail Desktop OAuth client, and authorize the Gmail account:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from gclientid import authorize_gmail, connect_browser, create_gmail_client, create_project
|
|
29
|
+
|
|
30
|
+
cdp, page = await connect_browser()
|
|
31
|
+
project_id = 'gclientids-your-unique-suffix'
|
|
32
|
+
|
|
33
|
+
await create_project(page, project_id, name='gclientids')
|
|
34
|
+
await create_gmail_client(page, project_id, 'oauth-client.json')
|
|
35
|
+
await authorize_gmail(cdp, 'oauth-client.json', 'oauth-token.json')
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`connect_browser()` uses the normal Chrome profile by default. Chrome gives you 60 seconds to approve the debugging connection. The function opens a new tab instead of navigating the currently focused tab. To use the separate CDP Chrome profile on port 9223 instead:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
cdp, page = await connect_browser(default_browser=False)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`create_gmail_client` enables the Gmail API, configures an External OAuth application, adds the full Gmail scope, publishes the unverified application, creates a Desktop client, and writes Google's installed-app client JSON. It selects an email offered by the signed-in Google account for the support and contact fields. It does not require an email or username argument.
|
|
45
|
+
|
|
46
|
+
On a new Google Auth Platform application, `create_gmail_client` pauses at Google's API Services terms screen. Complete that visible screen in Chrome and the function continues. Pass `accept_terms=True` to accept and submit that screen automatically:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
await create_gmail_client(page, project_id, 'oauth-client.json', accept_terms=True)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`authorize_gmail` opens Google's account and consent screens in a new Chrome tab. It selects the account automatically when Google offers one existing account, then approves the requested Gmail access. When Google offers multiple existing accounts, select one by display name or email substring:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
await authorize_gmail(cdp, 'oauth-client.json', 'oauth-token.json', account='j@example.com')
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The function uses a PKCE loopback flow and writes the access token, refresh token, granted scope, client ID, and creation time to `oauth-token.json`.
|
|
59
|
+
|
|
60
|
+
Both JSON files are created with mode `0600`. `create_gmail_client` refuses to overwrite an existing client file because Google only exposes the Desktop client secret at creation time. `authorize_gmail` replaces the token file when authorization succeeds. Keep both files out of git.
|
|
61
|
+
|
|
62
|
+
Project deletion is also available. Google keeps a deleted project recoverable for 30 days:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from gclientid import delete_project
|
|
66
|
+
|
|
67
|
+
await delete_project(page, project_id)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
See the [privacy policy](PRIVACY.md) for how Google user data is handled.
|
|
71
|
+
|
|
72
|
+
## Personal OAuth applications
|
|
73
|
+
|
|
74
|
+
The intended setup is one Google Cloud project and Desktop OAuth client per
|
|
75
|
+
developer. Configure its audience as **External**, publish it **In production**,
|
|
76
|
+
and leave it unverified. Google may say that the app "requires verification",
|
|
77
|
+
but personal-use applications can still authorize up to 100 distinct users.
|
|
78
|
+
Users see an unverified-app warning during initial authorization; verification
|
|
79
|
+
is only needed to remove that warning or exceed the lifetime user cap.
|
|
80
|
+
|
|
81
|
+
Do not leave a continuously running application in **Testing**. Testing requires
|
|
82
|
+
an explicit test-user email list, and authorizations requesting Gmail access
|
|
83
|
+
expire after seven days. An unverified app that is In production needs neither
|
|
84
|
+
the allowlist nor seven-day reauthorization.
|
|
85
|
+
|
|
86
|
+
The unrestricted Gmail scope is `https://mail.google.com/`. It is a restricted
|
|
87
|
+
scope and allows reading, composing, sending, and permanently deleting mail.
|
|
88
|
+
The application must request this scope during authorization even when it is
|
|
89
|
+
already listed on the Google Auth Platform Data Access page.
|
|
90
|
+
|
|
91
|
+
Google currently requires a Desktop client's `client_secret` during the token
|
|
92
|
+
exchange, including when PKCE is used. The secret is shown only when the client
|
|
93
|
+
is created, so capture or download the client JSON immediately. Desktop software
|
|
94
|
+
cannot keep this value confidential, but it must still be kept out of git along
|
|
95
|
+
with user access and refresh tokens.
|
|
96
|
+
|
|
97
|
+
## Development
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
pip install -e .[dev]
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Versioning
|
|
104
|
+
|
|
105
|
+
Version lives in `gclientid/__init__.py` as `__version__`.
|
|
106
|
+
Bump it with:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
ship-bump --part 2 # patch
|
|
110
|
+
ship-bump --part 1 # minor
|
|
111
|
+
ship-bump --part 0 # major
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Release
|
|
115
|
+
|
|
116
|
+
1) Ensure your GitHub issues are labeled (`bug`, `enhancement`, `breaking`).
|
|
117
|
+
2) Run:
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
ship-release
|
|
121
|
+
```
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# gclientid
|
|
2
|
+
|
|
3
|
+
Create Google OAuth desktop client IDs locally, without requiring `gcloud`.
|
|
4
|
+
|
|
5
|
+
`gclientid` uses an existing signed-in [fastcdp](https://github.com/AnswerDotAI/fastcdp) Chrome session. Enable **Allow remote debugging** in `chrome://inspect/#remote-debugging`, then create a globally unique project ID, provision its Gmail Desktop OAuth client, and authorize the Gmail account:
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
from gclientid import authorize_gmail, connect_browser, create_gmail_client, create_project
|
|
9
|
+
|
|
10
|
+
cdp, page = await connect_browser()
|
|
11
|
+
project_id = 'gclientids-your-unique-suffix'
|
|
12
|
+
|
|
13
|
+
await create_project(page, project_id, name='gclientids')
|
|
14
|
+
await create_gmail_client(page, project_id, 'oauth-client.json')
|
|
15
|
+
await authorize_gmail(cdp, 'oauth-client.json', 'oauth-token.json')
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`connect_browser()` uses the normal Chrome profile by default. Chrome gives you 60 seconds to approve the debugging connection. The function opens a new tab instead of navigating the currently focused tab. To use the separate CDP Chrome profile on port 9223 instead:
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
cdp, page = await connect_browser(default_browser=False)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`create_gmail_client` enables the Gmail API, configures an External OAuth application, adds the full Gmail scope, publishes the unverified application, creates a Desktop client, and writes Google's installed-app client JSON. It selects an email offered by the signed-in Google account for the support and contact fields. It does not require an email or username argument.
|
|
25
|
+
|
|
26
|
+
On a new Google Auth Platform application, `create_gmail_client` pauses at Google's API Services terms screen. Complete that visible screen in Chrome and the function continues. Pass `accept_terms=True` to accept and submit that screen automatically:
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
await create_gmail_client(page, project_id, 'oauth-client.json', accept_terms=True)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`authorize_gmail` opens Google's account and consent screens in a new Chrome tab. It selects the account automatically when Google offers one existing account, then approves the requested Gmail access. When Google offers multiple existing accounts, select one by display name or email substring:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
await authorize_gmail(cdp, 'oauth-client.json', 'oauth-token.json', account='j@example.com')
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The function uses a PKCE loopback flow and writes the access token, refresh token, granted scope, client ID, and creation time to `oauth-token.json`.
|
|
39
|
+
|
|
40
|
+
Both JSON files are created with mode `0600`. `create_gmail_client` refuses to overwrite an existing client file because Google only exposes the Desktop client secret at creation time. `authorize_gmail` replaces the token file when authorization succeeds. Keep both files out of git.
|
|
41
|
+
|
|
42
|
+
Project deletion is also available. Google keeps a deleted project recoverable for 30 days:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from gclientid import delete_project
|
|
46
|
+
|
|
47
|
+
await delete_project(page, project_id)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
See the [privacy policy](PRIVACY.md) for how Google user data is handled.
|
|
51
|
+
|
|
52
|
+
## Personal OAuth applications
|
|
53
|
+
|
|
54
|
+
The intended setup is one Google Cloud project and Desktop OAuth client per
|
|
55
|
+
developer. Configure its audience as **External**, publish it **In production**,
|
|
56
|
+
and leave it unverified. Google may say that the app "requires verification",
|
|
57
|
+
but personal-use applications can still authorize up to 100 distinct users.
|
|
58
|
+
Users see an unverified-app warning during initial authorization; verification
|
|
59
|
+
is only needed to remove that warning or exceed the lifetime user cap.
|
|
60
|
+
|
|
61
|
+
Do not leave a continuously running application in **Testing**. Testing requires
|
|
62
|
+
an explicit test-user email list, and authorizations requesting Gmail access
|
|
63
|
+
expire after seven days. An unverified app that is In production needs neither
|
|
64
|
+
the allowlist nor seven-day reauthorization.
|
|
65
|
+
|
|
66
|
+
The unrestricted Gmail scope is `https://mail.google.com/`. It is a restricted
|
|
67
|
+
scope and allows reading, composing, sending, and permanently deleting mail.
|
|
68
|
+
The application must request this scope during authorization even when it is
|
|
69
|
+
already listed on the Google Auth Platform Data Access page.
|
|
70
|
+
|
|
71
|
+
Google currently requires a Desktop client's `client_secret` during the token
|
|
72
|
+
exchange, including when PKCE is used. The secret is shown only when the client
|
|
73
|
+
is created, so capture or download the client JSON immediately. Desktop software
|
|
74
|
+
cannot keep this value confidential, but it must still be kept out of git along
|
|
75
|
+
with user access and refresh tokens.
|
|
76
|
+
|
|
77
|
+
## Development
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
pip install -e .[dev]
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Versioning
|
|
84
|
+
|
|
85
|
+
Version lives in `gclientid/__init__.py` as `__version__`.
|
|
86
|
+
Bump it with:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
ship-bump --part 2 # patch
|
|
90
|
+
ship-bump --part 1 # minor
|
|
91
|
+
ship-bump --part 0 # major
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Release
|
|
95
|
+
|
|
96
|
+
1) Ensure your GitHub issues are labeled (`bug`, `enhancement`, `breaking`).
|
|
97
|
+
2) Run:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
ship-release
|
|
101
|
+
```
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import asyncio, base64, hashlib, json, os, secrets
|
|
2
|
+
from datetime import datetime, timezone
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from urllib.parse import parse_qs, urlencode, urlparse
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
from fastcdp import CDP, Page
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
GMAIL_SCOPE = 'https://mail.google.com/'
|
|
11
|
+
HOME_URL = 'https://answerdotai.github.io/gclientid/'
|
|
12
|
+
PRIVACY_URL = f'{HOME_URL}privacy/'
|
|
13
|
+
DOMAIN = 'answerdotai.github.io'
|
|
14
|
+
AUTH_URI = 'https://accounts.google.com/o/oauth2/auth'
|
|
15
|
+
TOKEN_URI = 'https://oauth2.googleapis.com/token'
|
|
16
|
+
CERT_URI = 'https://www.googleapis.com/oauth2/v1/certs'
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
async def connect_browser(
|
|
20
|
+
default_browser:bool=True, # Use normal Chrome instead of CDP Chrome?
|
|
21
|
+
timeout:int=60, # Seconds to wait for normal Chrome's approval
|
|
22
|
+
) -> tuple[CDP, Page]:
|
|
23
|
+
"Connect to Chrome and return its connection and a new page"
|
|
24
|
+
try: cdp = await (CDP.connect(timeout=timeout) if default_browser else CDP.remote())
|
|
25
|
+
except FileNotFoundError: raise RuntimeError('Enable Allow remote debugging in chrome://inspect/#remote-debugging, then retry') from None
|
|
26
|
+
return cdp, await cdp.new_page()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _write_json(path:str|Path, data:dict) -> Path:
|
|
30
|
+
"Write private JSON and return its path"
|
|
31
|
+
path = Path(path)
|
|
32
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
33
|
+
path.write_text(json.dumps(data, indent=2) + '\n')
|
|
34
|
+
os.chmod(path, 0o600)
|
|
35
|
+
return path
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
async def _enable_gmail(page:Page, project_id:str, timeout:int):
|
|
39
|
+
url = f'https://console.cloud.google.com/apis/library/gmail.googleapis.com?project={project_id}'
|
|
40
|
+
await page.goto(url, timeout=timeout)
|
|
41
|
+
tree = await page.wait_for_ax('heading', 'Gmail API', timeout=timeout)
|
|
42
|
+
enable = tree.find('button', 'enable this API')
|
|
43
|
+
if enable:
|
|
44
|
+
await page.click(enable.find_id())
|
|
45
|
+
await page.wait_for_ax('button', 'Disable API', timeout=timeout)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
async def _setup_auth(page:Page, project_id:str, name:str, accept_terms:bool, timeout:int, terms_timeout:int):
|
|
49
|
+
await page.goto(f'https://console.cloud.google.com/auth/overview?project={project_id}', timeout=timeout)
|
|
50
|
+
tree = await page.ax_tree()
|
|
51
|
+
start = tree.find('link', 'Get started')
|
|
52
|
+
if not start: return
|
|
53
|
+
|
|
54
|
+
await page.click(start.find_id())
|
|
55
|
+
tree = await page.wait_for_ax('heading', 'App Information', timeout=timeout)
|
|
56
|
+
await page.fill_text(tree.find_id('textbox', 'App name'), name)
|
|
57
|
+
await page.wait_for(r'''[...document.querySelectorAll('[role="combobox"]')].some(
|
|
58
|
+
x => x.getAttribute('aria-label') === 'User support email' && x.getAttribute('aria-disabled') !== 'true')''', timeout=timeout)
|
|
59
|
+
tree = await page.ax_tree()
|
|
60
|
+
await page.click(tree.find_id('combobox', 'User support email'))
|
|
61
|
+
tree = await page.wait_for_ax('option', timeout=timeout)
|
|
62
|
+
email = next((n.name for n in tree.find_all('option') if '@' in n.name), None)
|
|
63
|
+
if not email: raise RuntimeError('Google did not offer a support email')
|
|
64
|
+
await page.click(tree.find_id('option', email))
|
|
65
|
+
|
|
66
|
+
tree = await page.ax_tree()
|
|
67
|
+
await page.click(tree.find('main').find('form').find_id('button', 'Next'))
|
|
68
|
+
tree = await page.wait_for_ax('heading', 'Audience, step 2 of 4, in progress', timeout=timeout)
|
|
69
|
+
await page.click(tree.find('main').find('form').find_id('radio', 'External'))
|
|
70
|
+
tree = await page.ax_tree()
|
|
71
|
+
await page.click(tree.find('main').find('form').find_id('button', 'Next'))
|
|
72
|
+
|
|
73
|
+
tree = await page.wait_for_ax('heading', 'Contact Information, step 3 of 4, in progress', timeout=timeout)
|
|
74
|
+
contact = tree.find('main').find('form').find_id('textbox', 'Text field for emails')
|
|
75
|
+
await page.fill_text(contact, email)
|
|
76
|
+
await page.input.dispatchKeyEvent(type='rawKeyDown', key='Enter', code='Enter', windowsVirtualKeyCode=13)
|
|
77
|
+
await page.input.dispatchKeyEvent(type='keyUp', key='Enter', code='Enter', windowsVirtualKeyCode=13)
|
|
78
|
+
tree = await page.ax_tree()
|
|
79
|
+
await page.click(tree.find('main').find('form').find_id('button', 'Next'))
|
|
80
|
+
|
|
81
|
+
tree = await page.wait_for_ax('heading', 'Finish, step 4 of 4, in progress', timeout=timeout)
|
|
82
|
+
if accept_terms:
|
|
83
|
+
await page.click(tree.find('main').find('form').find_id('checkbox', 'I agree'))
|
|
84
|
+
tree = await page.ax_tree()
|
|
85
|
+
await page.click(tree.find('main').find('form').find_id('button', 'Continue'))
|
|
86
|
+
tree = await page.wait_for_ax('button', 'Create', timeout=timeout)
|
|
87
|
+
await page.click(tree.find('main').find('form').find_id('button', 'Create'))
|
|
88
|
+
await page.wait_for_ax('heading', 'OAuth Overview', timeout=terms_timeout)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
async def _set_branding(page:Page, project_id:str, timeout:int):
|
|
92
|
+
await page.goto(f'https://console.cloud.google.com/auth/branding?project={project_id}', timeout=timeout)
|
|
93
|
+
tree = await page.wait_for_ax('heading', 'Branding', timeout=timeout)
|
|
94
|
+
await page.fill_text(tree.find_id('textbox', 'Application home page'), HOME_URL)
|
|
95
|
+
await page.fill_text(tree.find_id('textbox', 'Application privacy policy link'), PRIVACY_URL)
|
|
96
|
+
tree = await page.ax_tree()
|
|
97
|
+
if not tree.find(name=DOMAIN):
|
|
98
|
+
domains = tree.find('main').find('form').find_all('textbox', 'Authorized domain')
|
|
99
|
+
if not domains:
|
|
100
|
+
await page.click(tree.find_id('button', 'Add domain'))
|
|
101
|
+
tree = await page.wait_for_ax('textbox', 'Authorized domain', timeout=timeout)
|
|
102
|
+
domains = tree.find('main').find('form').find_all('textbox', 'Authorized domain')
|
|
103
|
+
await page.fill_text(domains[-1].find_id(), DOMAIN)
|
|
104
|
+
tree = await page.ax_tree()
|
|
105
|
+
await page.click(tree.find('main').find('form').find_id('button', 'Save'))
|
|
106
|
+
await page.wait_for_ready(timeout=timeout)
|
|
107
|
+
if (await page.ax_tree()).find('dialog', 'Error dialog'): raise RuntimeError('Google rejected the OAuth branding settings')
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
async def _set_scope(page:Page, project_id:str, timeout:int):
|
|
111
|
+
await page.goto(f'https://console.cloud.google.com/auth/scopes?project={project_id}', timeout=timeout)
|
|
112
|
+
tree = await page.wait_for_ax('heading', 'Data Access', timeout=timeout)
|
|
113
|
+
if any(GMAIL_SCOPE in n.name.replace(' ', '') for n in tree.find_all('row')): return
|
|
114
|
+
await page.click(tree.find_id('button', 'Add or remove scopes'))
|
|
115
|
+
tree = await page.wait_for_ax('dialog', 'Update selected scopes', timeout=timeout)
|
|
116
|
+
dialog = tree.find('dialog', 'Update selected scopes')
|
|
117
|
+
await page.fill_text(dialog.find_id('textbox', 'Manually paste scopes'), GMAIL_SCOPE)
|
|
118
|
+
tree = await page.ax_tree()
|
|
119
|
+
await page.click(tree.find('dialog', 'Update selected scopes').find_id('button', 'Add to table'))
|
|
120
|
+
tree = await page.wait_for_ax('row', 'https://mail', timeout=timeout)
|
|
121
|
+
await page.click(tree.find('dialog', 'Update selected scopes').find_id('button', 'Update'))
|
|
122
|
+
await page.wait_for_text('Update selected scopes', present=False, timeout=timeout)
|
|
123
|
+
tree = await page.ax_tree()
|
|
124
|
+
await page.click(tree.find('main').find('form').find_id('button', 'Save'))
|
|
125
|
+
await page.wait_for_ready(timeout=timeout)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
async def _publish(page:Page, project_id:str, timeout:int):
|
|
129
|
+
await page.goto(f'https://console.cloud.google.com/auth/audience?project={project_id}', timeout=timeout)
|
|
130
|
+
tree = await page.wait_for_ax('heading', 'Audience', timeout=timeout)
|
|
131
|
+
if tree.find(name='In production'): return
|
|
132
|
+
await page.click(tree.find_id('button', 'Publish app'))
|
|
133
|
+
tree = await page.wait_for_ax('alertdialog', 'Push to production?', timeout=timeout)
|
|
134
|
+
await page.click(tree.find('alertdialog', 'Push to production?').find_id('button', 'Confirm'))
|
|
135
|
+
await page.wait_for_ax(name='In production', timeout=timeout)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
async def create_gmail_client(
|
|
139
|
+
page:Page, # Signed-in Google Cloud Console page
|
|
140
|
+
project_id:str, # Existing Google Cloud project ID
|
|
141
|
+
path:str|Path='oauth-client.json', # Destination for Google's installed-app client JSON
|
|
142
|
+
name:str='gclientids', # OAuth application and Desktop client name
|
|
143
|
+
accept_terms:bool=False, # Accept Google's API Services terms without pausing?
|
|
144
|
+
timeout:int=60, # Seconds to wait for each Console operation
|
|
145
|
+
terms_timeout:int=600, # Seconds to wait while the developer handles the terms screen
|
|
146
|
+
) -> dict:
|
|
147
|
+
"Configure Gmail OAuth, create a Desktop client, and save its client JSON"
|
|
148
|
+
path = Path(path)
|
|
149
|
+
if path.exists(): raise FileExistsError(path)
|
|
150
|
+
await _enable_gmail(page, project_id, timeout)
|
|
151
|
+
await _setup_auth(page, project_id, name, accept_terms, timeout, terms_timeout)
|
|
152
|
+
await _set_branding(page, project_id, timeout)
|
|
153
|
+
await _set_scope(page, project_id, timeout)
|
|
154
|
+
await _publish(page, project_id, timeout)
|
|
155
|
+
|
|
156
|
+
await page.goto(f'https://console.cloud.google.com/auth/clients/create?project={project_id}', timeout=timeout)
|
|
157
|
+
tree = await page.wait_for_ax('heading', 'Create OAuth client ID', timeout=timeout)
|
|
158
|
+
await page.click(tree.find_id('combobox', 'Application type'))
|
|
159
|
+
tree = await page.wait_for_ax('option', 'Desktop app', timeout=timeout)
|
|
160
|
+
await page.click(tree.find_id('option', 'Desktop app'))
|
|
161
|
+
tree = await page.wait_for_ax('textbox', 'Name', timeout=timeout)
|
|
162
|
+
form = tree.find('main').find('form').find('form')
|
|
163
|
+
await page.fill_text(form.find_id('textbox', 'Name'), name)
|
|
164
|
+
tree = await page.ax_tree()
|
|
165
|
+
await page.click(tree.find('main').find('form').find('form').find_id('button', 'Create'))
|
|
166
|
+
tree = await page.wait_for_ax('dialog', 'OAuth client created', timeout=timeout)
|
|
167
|
+
dialog = tree.find('dialog', 'OAuth client created')
|
|
168
|
+
values = [n.name.removeprefix('Copy to clipboard: ') for n in dialog.find_all('button', 'Copy to clipboard:')]
|
|
169
|
+
client_id = next((v for v in values if v.endswith('.apps.googleusercontent.com')), None)
|
|
170
|
+
client_secret = next((v for v in values if v != client_id), None)
|
|
171
|
+
if not client_id or not client_secret: raise RuntimeError('Google did not expose the new client credentials')
|
|
172
|
+
config = {'installed': {
|
|
173
|
+
'client_id': client_id, 'project_id': project_id, 'auth_uri': AUTH_URI,
|
|
174
|
+
'token_uri': TOKEN_URI, 'auth_provider_x509_cert_url': CERT_URI,
|
|
175
|
+
'client_secret': client_secret, 'redirect_uris': ['http://localhost']}}
|
|
176
|
+
_write_json(path, config)
|
|
177
|
+
await page.click(dialog.find_id('button', 'OK'))
|
|
178
|
+
return config
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
async def authorize_gmail(
|
|
182
|
+
cdp:CDP, # CDP connection to a Chrome session signed into Google
|
|
183
|
+
client_path:str|Path='oauth-client.json', # Installed-app client JSON from create_gmail_client
|
|
184
|
+
token_path:str|Path='oauth-token.json', # Destination for access and refresh token JSON
|
|
185
|
+
scope:str=GMAIL_SCOPE, # OAuth scope to request
|
|
186
|
+
account:str=None, # Display-name or email substring when Google offers multiple accounts
|
|
187
|
+
timeout:int=600, # Seconds to wait for browser authorization
|
|
188
|
+
) -> dict:
|
|
189
|
+
"Authorize Gmail in Chrome and save refreshable token JSON"
|
|
190
|
+
client = json.loads(Path(client_path).read_text())['installed']
|
|
191
|
+
callback = asyncio.get_running_loop().create_future()
|
|
192
|
+
|
|
193
|
+
async def receive_oauth(reader, writer):
|
|
194
|
+
request = (await reader.readline()).decode()
|
|
195
|
+
query = parse_qs(urlparse(request.split()[1]).query)
|
|
196
|
+
while await reader.readline() not in (b'\r\n', b''): pass
|
|
197
|
+
if not callback.done(): callback.set_result(query)
|
|
198
|
+
body = b'<h1>Authorization received</h1><p>You can close this tab.</p>'
|
|
199
|
+
headers = b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: ' + str(len(body)).encode() + b'\r\n\r\n'
|
|
200
|
+
writer.write(headers + body)
|
|
201
|
+
await writer.drain()
|
|
202
|
+
writer.close()
|
|
203
|
+
|
|
204
|
+
server = await asyncio.start_server(receive_oauth, '127.0.0.1', 0)
|
|
205
|
+
port = server.sockets[0].getsockname()[1]
|
|
206
|
+
verifier = secrets.token_urlsafe(64)
|
|
207
|
+
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b'=').decode()
|
|
208
|
+
state = secrets.token_urlsafe(24)
|
|
209
|
+
redirect_uri = f'http://127.0.0.1:{port}/'
|
|
210
|
+
auth_url = 'https://accounts.google.com/o/oauth2/v2/auth?' + urlencode({
|
|
211
|
+
'client_id': client['client_id'], 'redirect_uri': redirect_uri, 'response_type': 'code',
|
|
212
|
+
'scope': scope, 'access_type': 'offline', 'prompt': 'consent',
|
|
213
|
+
'code_challenge': challenge, 'code_challenge_method': 'S256', 'state': state})
|
|
214
|
+
try:
|
|
215
|
+
auth_page = await cdp.new_page()
|
|
216
|
+
await cdp.target.activateTarget(targetId=auth_page.t)
|
|
217
|
+
await auth_page.goto(auth_url, timeout=60)
|
|
218
|
+
tree = await auth_page.ax_tree()
|
|
219
|
+
if tree.find('heading', 'Choose an account'):
|
|
220
|
+
accounts = [n for n in tree.find('list').find_all('link') if 'Use another account' not in n.name]
|
|
221
|
+
matches = accounts if account is None else [n for n in accounts if account.casefold() in n.name.casefold()]
|
|
222
|
+
if len(matches) != 1:
|
|
223
|
+
choices = ', '.join(n.name for n in accounts)
|
|
224
|
+
raise ValueError(f'Choose one Google account with account=: {choices}')
|
|
225
|
+
await auth_page.click(matches[0].find_id())
|
|
226
|
+
tree = await auth_page.wait_for_ax('button', 'Allow', timeout=timeout)
|
|
227
|
+
await auth_page.click(tree.find_id('button', 'Allow'))
|
|
228
|
+
query = await asyncio.wait_for(callback, timeout=timeout)
|
|
229
|
+
finally:
|
|
230
|
+
server.close()
|
|
231
|
+
await server.wait_closed()
|
|
232
|
+
if query.get('state') != [state]: raise RuntimeError('OAuth state did not match')
|
|
233
|
+
if 'error' in query: raise RuntimeError(query['error'][0])
|
|
234
|
+
|
|
235
|
+
async with httpx.AsyncClient() as http:
|
|
236
|
+
response = await http.post(TOKEN_URI, data={
|
|
237
|
+
'client_id': client['client_id'], 'client_secret': client['client_secret'],
|
|
238
|
+
'code': query['code'][0], 'code_verifier': verifier, 'redirect_uri': redirect_uri,
|
|
239
|
+
'grant_type': 'authorization_code'})
|
|
240
|
+
response.raise_for_status()
|
|
241
|
+
token = response.json()
|
|
242
|
+
token['client_id'] = client['client_id']
|
|
243
|
+
token['created_at'] = datetime.now(timezone.utc).isoformat()
|
|
244
|
+
_write_json(token_path, token)
|
|
245
|
+
return token
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from urllib.parse import quote
|
|
2
|
+
|
|
3
|
+
from fastcdp import Page
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
CREATE_URL = 'https://console.cloud.google.com/projectcreate'
|
|
7
|
+
SETTINGS_URL = 'https://console.cloud.google.com/iam-admin/settings'
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
async def _replace_text(page:Page, node_id:int, text:str):
|
|
11
|
+
"Replace the contents of a text control"
|
|
12
|
+
await page.fill_text(node_id, text)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
async def create_project(page:Page, project_id:str, name:str=None, timeout:int=30) -> str:
|
|
16
|
+
"Create a Google Cloud project and return its project ID"
|
|
17
|
+
await page.goto(CREATE_URL, timeout=timeout)
|
|
18
|
+
tree = await page.wait_for_ax('textbox', 'Project name', timeout=timeout)
|
|
19
|
+
form = tree.find('main').find('form')
|
|
20
|
+
await _replace_text(page, form.find_id('textbox', 'Project name'), name or project_id)
|
|
21
|
+
|
|
22
|
+
tree = await page.wait_for_ax('button', 'Edit the project id.', timeout=timeout)
|
|
23
|
+
form = tree.find('main').find('form')
|
|
24
|
+
await page.click(form.find_id('button', 'Edit the project id.'))
|
|
25
|
+
tree = await page.wait_for_ax('textbox', 'Project ID', timeout=timeout)
|
|
26
|
+
form = tree.find('main').find('form')
|
|
27
|
+
await _replace_text(page, form.find_id('textbox', 'Project ID'), project_id)
|
|
28
|
+
|
|
29
|
+
tree = await page.wait_for_ax('button', 'Create', timeout=timeout)
|
|
30
|
+
await page.click(tree.find('main').find('form').find_id('button', 'Create'))
|
|
31
|
+
await page.wait_for_ax('button', f'Navigate to {project_id} project', timeout=timeout)
|
|
32
|
+
return project_id
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
async def delete_project(page:Page, project_id:str, timeout:int=30) -> str:
|
|
36
|
+
"Shut down a Google Cloud project and return its project ID"
|
|
37
|
+
url = f'{SETTINGS_URL}?project={quote(project_id, safe="")}'
|
|
38
|
+
await page.goto(url, timeout=timeout)
|
|
39
|
+
tree = await page.wait_for_ax('button', 'Shut down', timeout=timeout)
|
|
40
|
+
await page.click(tree.find_id('button', 'Shut down'))
|
|
41
|
+
|
|
42
|
+
tree = await page.wait_for_ax('dialog', 'Shut down project', timeout=timeout)
|
|
43
|
+
dialog = tree.find('dialog', 'Shut down project')
|
|
44
|
+
await _replace_text(page, dialog.find_id('textbox', 'Project ID'), project_id)
|
|
45
|
+
|
|
46
|
+
tree = await page.ax_tree()
|
|
47
|
+
dialog = tree.find('dialog', 'Shut down project')
|
|
48
|
+
await page.click(dialog.find_id('button', 'Shut down anyway'))
|
|
49
|
+
await page.wait_for_ax('dialog', 'Project is pending deletion', timeout=timeout)
|
|
50
|
+
return project_id
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gclientid
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Create Google OAuth desktop client IDs locally
|
|
5
|
+
Author-email: Jeremy Howard <github@jhoward.fastmail.fm>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/AnswerDotAI/gclientid
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: fastcdp>=0.0.8
|
|
14
|
+
Requires-Dist: httpx
|
|
15
|
+
Provides-Extra: dev
|
|
16
|
+
Requires-Dist: fastship; extra == "dev"
|
|
17
|
+
Requires-Dist: build; extra == "dev"
|
|
18
|
+
Requires-Dist: twine; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# gclientid
|
|
22
|
+
|
|
23
|
+
Create Google OAuth desktop client IDs locally, without requiring `gcloud`.
|
|
24
|
+
|
|
25
|
+
`gclientid` uses an existing signed-in [fastcdp](https://github.com/AnswerDotAI/fastcdp) Chrome session. Enable **Allow remote debugging** in `chrome://inspect/#remote-debugging`, then create a globally unique project ID, provision its Gmail Desktop OAuth client, and authorize the Gmail account:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from gclientid import authorize_gmail, connect_browser, create_gmail_client, create_project
|
|
29
|
+
|
|
30
|
+
cdp, page = await connect_browser()
|
|
31
|
+
project_id = 'gclientids-your-unique-suffix'
|
|
32
|
+
|
|
33
|
+
await create_project(page, project_id, name='gclientids')
|
|
34
|
+
await create_gmail_client(page, project_id, 'oauth-client.json')
|
|
35
|
+
await authorize_gmail(cdp, 'oauth-client.json', 'oauth-token.json')
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`connect_browser()` uses the normal Chrome profile by default. Chrome gives you 60 seconds to approve the debugging connection. The function opens a new tab instead of navigating the currently focused tab. To use the separate CDP Chrome profile on port 9223 instead:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
cdp, page = await connect_browser(default_browser=False)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`create_gmail_client` enables the Gmail API, configures an External OAuth application, adds the full Gmail scope, publishes the unverified application, creates a Desktop client, and writes Google's installed-app client JSON. It selects an email offered by the signed-in Google account for the support and contact fields. It does not require an email or username argument.
|
|
45
|
+
|
|
46
|
+
On a new Google Auth Platform application, `create_gmail_client` pauses at Google's API Services terms screen. Complete that visible screen in Chrome and the function continues. Pass `accept_terms=True` to accept and submit that screen automatically:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
await create_gmail_client(page, project_id, 'oauth-client.json', accept_terms=True)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`authorize_gmail` opens Google's account and consent screens in a new Chrome tab. It selects the account automatically when Google offers one existing account, then approves the requested Gmail access. When Google offers multiple existing accounts, select one by display name or email substring:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
await authorize_gmail(cdp, 'oauth-client.json', 'oauth-token.json', account='j@example.com')
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The function uses a PKCE loopback flow and writes the access token, refresh token, granted scope, client ID, and creation time to `oauth-token.json`.
|
|
59
|
+
|
|
60
|
+
Both JSON files are created with mode `0600`. `create_gmail_client` refuses to overwrite an existing client file because Google only exposes the Desktop client secret at creation time. `authorize_gmail` replaces the token file when authorization succeeds. Keep both files out of git.
|
|
61
|
+
|
|
62
|
+
Project deletion is also available. Google keeps a deleted project recoverable for 30 days:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from gclientid import delete_project
|
|
66
|
+
|
|
67
|
+
await delete_project(page, project_id)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
See the [privacy policy](PRIVACY.md) for how Google user data is handled.
|
|
71
|
+
|
|
72
|
+
## Personal OAuth applications
|
|
73
|
+
|
|
74
|
+
The intended setup is one Google Cloud project and Desktop OAuth client per
|
|
75
|
+
developer. Configure its audience as **External**, publish it **In production**,
|
|
76
|
+
and leave it unverified. Google may say that the app "requires verification",
|
|
77
|
+
but personal-use applications can still authorize up to 100 distinct users.
|
|
78
|
+
Users see an unverified-app warning during initial authorization; verification
|
|
79
|
+
is only needed to remove that warning or exceed the lifetime user cap.
|
|
80
|
+
|
|
81
|
+
Do not leave a continuously running application in **Testing**. Testing requires
|
|
82
|
+
an explicit test-user email list, and authorizations requesting Gmail access
|
|
83
|
+
expire after seven days. An unverified app that is In production needs neither
|
|
84
|
+
the allowlist nor seven-day reauthorization.
|
|
85
|
+
|
|
86
|
+
The unrestricted Gmail scope is `https://mail.google.com/`. It is a restricted
|
|
87
|
+
scope and allows reading, composing, sending, and permanently deleting mail.
|
|
88
|
+
The application must request this scope during authorization even when it is
|
|
89
|
+
already listed on the Google Auth Platform Data Access page.
|
|
90
|
+
|
|
91
|
+
Google currently requires a Desktop client's `client_secret` during the token
|
|
92
|
+
exchange, including when PKCE is used. The secret is shown only when the client
|
|
93
|
+
is created, so capture or download the client JSON immediately. Desktop software
|
|
94
|
+
cannot keep this value confidential, but it must still be kept out of git along
|
|
95
|
+
with user access and refresh tokens.
|
|
96
|
+
|
|
97
|
+
## Development
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
pip install -e .[dev]
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Versioning
|
|
104
|
+
|
|
105
|
+
Version lives in `gclientid/__init__.py` as `__version__`.
|
|
106
|
+
Bump it with:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
ship-bump --part 2 # patch
|
|
110
|
+
ship-bump --part 1 # minor
|
|
111
|
+
ship-bump --part 0 # major
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Release
|
|
115
|
+
|
|
116
|
+
1) Ensure your GitHub issues are labeled (`bug`, `enhancement`, `breaking`).
|
|
117
|
+
2) Run:
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
ship-release
|
|
121
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
CHANGELOG.md
|
|
2
|
+
LICENSE
|
|
3
|
+
MANIFEST.in
|
|
4
|
+
README.md
|
|
5
|
+
pyproject.toml
|
|
6
|
+
gclientid/__init__.py
|
|
7
|
+
gclientid/oauth.py
|
|
8
|
+
gclientid/projects.py
|
|
9
|
+
gclientid.egg-info/PKG-INFO
|
|
10
|
+
gclientid.egg-info/SOURCES.txt
|
|
11
|
+
gclientid.egg-info/dependency_links.txt
|
|
12
|
+
gclientid.egg-info/requires.txt
|
|
13
|
+
gclientid.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
gclientid
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "gclientid"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Create Google OAuth desktop client IDs locally"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "Apache-2.0" }
|
|
12
|
+
authors = [{name = "Jeremy Howard", email = "github@jhoward.fastmail.fm"}]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
dependencies = ["fastcdp>=0.0.8", "httpx"]
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
dev = [
|
|
22
|
+
"fastship",
|
|
23
|
+
"build",
|
|
24
|
+
"twine",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/AnswerDotAI/gclientid"
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.dynamic]
|
|
31
|
+
version = { attr = "gclientid.__version__" }
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.packages.find]
|
|
34
|
+
include = ["gclientid"]
|