Skip to content

Commit 2581479

Browse files
committed
Update troubleshooting docs for Python
1 parent 1aa1cd4 commit 2581479

File tree

3 files changed

+19
-34
lines changed

3 files changed

+19
-34
lines changed

README.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -322,30 +322,6 @@ regardless of the authentication mechanism.
322322
"token_format" is "id_token".
323323
324324
325-
## Python Usage Note
326-
327-
When using Workload Identity Federation with Python libraries (e.g., `google-auth`), you may encounter errors when trying to refresh credentials to get an ID token. This is because the Google Auth library requires scopes to be set when refreshing credentials for impersonation.
328-
329-
If you need an ID token in Python, you have two options:
330-
331-
1. **Use the `token_format` parameter** (recommended): Generate the ID token directly with this action and use it as an environment variable in your Python code.
332-
333-
2. **Add scopes before refreshing**: If using default credentials, add the required scopes before refreshing:
334-
335-
```python
336-
from google.auth import default
337-
from google.auth.transport.requests import Request
338-
339-
credentials, project = default()
340-
credentials = credentials.with_scopes(
341-
["https://www.googleapis.com/auth/cloud-platform"]
342-
)
343-
credentials.refresh(request=Request())
344-
```
345-
346-
For more details and examples, see the [Troubleshooting guide](docs/TROUBLESHOOTING.md#cannot-refresh-credentials-to-retrieve-an-id-token) and [Examples](docs/EXAMPLES.md#using-default-credentials-with-scopes-in-python).
347-
348-
349325
<a id="setup"></a>
350326
## Setup
351327

docs/EXAMPLES.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,10 @@ jobs:
194194
python -c "
195195
import os
196196
import requests
197-
197+
198198
# ID token is available as environment variable
199199
id_token = os.environ.get('GOOGLE_ID_TOKEN', '${{ steps.auth.outputs.id_token }}')
200-
200+
201201
# Use the token to invoke a Cloud Run service
202202
response = requests.get(
203203
'https://myapp-uvehjacqzq.a.run.app',
@@ -209,7 +209,8 @@ jobs:
209209

210210
### Using Default Credentials with Scopes in Python
211211

212-
When using Workload Identity Federation with Python libraries, you may need to add scopes before refreshing credentials:
212+
When using Workload Identity Federation with Python libraries, you may need to
213+
add scopes before refreshing credentials:
213214

214215
```yaml
215216
jobs:
@@ -232,18 +233,18 @@ jobs:
232233
python -c "
233234
from google.auth import default
234235
from google.auth.transport.requests import Request
235-
236+
236237
# Get default credentials
237238
credentials, project = default()
238-
239+
239240
# Add scopes before refreshing for impersonation
240241
credentials = credentials.with_scopes(
241242
['https://www.googleapis.com/auth/cloud-platform']
242243
)
243-
244+
244245
# Refresh to get the token
245246
credentials.refresh(request=Request())
246-
247+
247248
# Now you can use the credentials
248249
print(f'Access token: {credentials.token}')
249250
if hasattr(credentials, 'id_token'):

docs/TROUBLESHOOTING.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ tool like `jq`:
230230
cat credentials.json | jq -r tostring
231231
```
232232

233+
<a name="cannot-refresh"></a>
234+
233235
## Cannot refresh credentials to retrieve an ID token
234236

235237
If you get an error like:
@@ -238,7 +240,9 @@ If you get an error like:
238240
google.auth.exceptions.RefreshError: ('Unable to acquire impersonated credentials', '{"error": {"code": 400, "message": "Request contains an invalid argument.", "status": "INVALID_ARGUMENT"}}')
239241
```
240242

241-
when trying to refresh credentials in Python code to get an ID token, this is usually because the credentials are missing required scopes. The Google Auth library requires scopes to be set when refreshing credentials for impersonation.
243+
when trying to refresh credentials in Python code to get an ID token, this is
244+
usually because the credentials are missing required scopes. The Google Auth
245+
library requires scopes to be set when refreshing credentials for impersonation.
242246

243247
To fix this issue, add the required scopes before refreshing:
244248

@@ -247,16 +251,19 @@ from google.auth import default
247251
from google.auth.transport.requests import Request
248252
249253
credentials, project = default()
254+
250255
# Add scopes before refreshing
251256
credentials = credentials.with_scopes(
252257
["https://www.googleapis.com/auth/cloud-platform"]
253258
)
254259
credentials.refresh(request=Request())
260+
255261
# Now you can access the ID token
256262
print(credentials.id_token)
257263
```
258264

259-
Alternatively, you can use the `token_format` parameter of this action to generate an ID token directly:
265+
Alternatively, you can use the `token_format` parameter of this action to
266+
generate an ID token directly:
260267

261268
```yaml
262269
- uses: 'google-github-actions/auth@v2'
@@ -267,7 +274,8 @@ Alternatively, you can use the `token_format` parameter of this action to genera
267274
id_token_audience: 'https://example.com'
268275
```
269276

270-
This will export the ID token as an environment variable that you can use in your Python code.
277+
This will export the ID token as an environment variable that you can use in
278+
your Python code.
271279

272280
## Organizational Policy Constraints
273281

0 commit comments

Comments
 (0)