@@ -187,6 +187,69 @@ jobs:
187187 run: |-
188188 curl https://myapp-uvehjacqzq.a.run.app \
189189 --header "Authorization: Bearer ${{ steps.auth.outputs.id_token }}"
190+
191+ # Example of using ID token in Python code
192+ - id: 'python-example'
193+ run: |-
194+ python -c "
195+ import os
196+ import requests
197+
198+ # ID token is available as environment variable
199+ id_token = os.environ.get('GOOGLE_ID_TOKEN', '${{ steps.auth.outputs.id_token }}')
200+
201+ # Use the token to invoke a Cloud Run service
202+ response = requests.get(
203+ 'https://myapp-uvehjacqzq.a.run.app',
204+ headers={'Authorization': f'Bearer {id_token}'}
205+ )
206+ print(response.text)
207+ "
208+ ` ` `
209+
210+ # ## Using Default Credentials with Scopes in Python
211+
212+ When using Workload Identity Federation with Python libraries, you may need to
213+ add scopes before refreshing credentials :
214+
215+ ` ` ` yaml
216+ jobs:
217+ job_id:
218+ permissions:
219+ contents: 'read'
220+ id-token: 'write'
221+
222+ steps:
223+ - uses: 'actions/checkout@v4'
224+
225+ - id: 'auth'
226+ uses: 'google-github-actions/auth@v2'
227+ with:
228+ workload_identity_provider: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider'
229+ service_account: 'my-service-account@my-project.iam.gserviceaccount.com'
230+
231+ - id: 'python-auth'
232+ run: |-
233+ python -c "
234+ from google.auth import default
235+ from google.auth.transport.requests import Request
236+
237+ # Get default credentials
238+ credentials, project = default()
239+
240+ # Add scopes before refreshing for impersonation
241+ credentials = credentials.with_scopes(
242+ ['https://www.googleapis.com/auth/cloud-platform']
243+ )
244+
245+ # Refresh to get the token
246+ credentials.refresh(request=Request())
247+
248+ # Now you can use the credentials
249+ print(f'Access token: {credentials.token}')
250+ if hasattr(credentials, 'id_token'):
251+ print(f'ID token: {credentials.id_token}')
252+ "
190253` ` `
191254
192255[github-markdown-toc] : https://github.blog/changelog/2021-04-13-table-of-contents-support-in-markdown-files/
0 commit comments