Skip to content
This repository was archived by the owner on Oct 29, 2023. It is now read-only.

Commit b251524

Browse files
codepriestzgcseh
authored andcommitted
feat: Add sample codes for cloud-iot-token-service generateAccessToken API
1 parent 432b6a2 commit b251524

File tree

11 files changed

+3147
-0
lines changed

11 files changed

+3147
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud IoT Token Api Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=iot/api-client/accesstoken_example/README.rst
8+
9+
10+
This directory contains samples for Google Cloud IoT Token Api. # Google Cloud IoT Core Python Device Federated Authentication example This sample app demonstrates the capabilites of Google Cloud IoT Core device federated authentication feature. Devices authenticated to Cloud IoT Core can use the [Token Service](https://cloud.google.com/iot/alpha/docs/reference/cloudiottoken/rest) federated authentication to request [OAuth 2.0 access tokens](https://developers.google.com/identity/protocols/oauth2) in exchange for their [Cloud IoT Core JWTs](https://cloud.google.com/iot/docs/how-tos/credentials/jwts). The OAuth 2.0 credentials can be used to call different [Google Cloud APIs](https://developers.google.com/identity/protocols/oauth2/scopes) with fine-grained permissions and access control using [Workload Identity Federation](https://cloud.google.com/iam/docs/workload-identity-federation). For more information, see https://cloud.google.com/iot/alpha/docs/how-tos/federated_auth
11+
12+
13+
14+
15+
.. _Google Cloud IoT Token Api: https://cloud.google.com/iot/docs
16+
17+
18+
19+
20+
21+
Setup
22+
-------------------------------------------------------------------------------
23+
24+
25+
Authentication
26+
++++++++++++++
27+
28+
This sample requires you to have authentication setup. Refer to the
29+
`Authentication Getting Started Guide`_ for instructions on setting up
30+
credentials for applications.
31+
32+
.. _Authentication Getting Started Guide:
33+
https://cloud.google.com/docs/authentication/getting-started
34+
35+
Install Dependencies
36+
++++++++++++++++++++
37+
38+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
39+
40+
.. code-block:: bash
41+
42+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
43+
44+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
45+
46+
.. _Python Development Environment Setup Guide:
47+
https://cloud.google.com/python/setup
48+
49+
#. Create a virtualenv. Samples are compatible with Python 3.6+.
50+
51+
.. code-block:: bash
52+
53+
$ virtualenv env
54+
$ source env/bin/activate
55+
56+
#. Install the dependencies needed to run the samples.
57+
58+
.. code-block:: bash
59+
60+
$ pip install -r requirements.txt
61+
62+
.. _pip: https://pip.pypa.io/
63+
.. _virtualenv: https://virtualenv.pypa.io/
64+
65+
Samples
66+
-------------------------------------------------------------------------------
67+
68+
accesstoken
69+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
70+
71+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
72+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=iot/api-client/accesstoken_example/accesstoken.py,iot/api-client/accesstoken_example/README.rst
73+
74+
75+
76+
77+
To run this sample:
78+
79+
.. code-block:: bash
80+
81+
$ python accesstoken.py
82+
83+
usage: accesstoken.py [-h] [--algorithm {RS256,ES256}]
84+
[--private_key_file PRIVATE_KEY_FILE]
85+
[--cloud_region CLOUD_REGION] [--device_id DEVICE_ID]
86+
[--scope SCOPE] [--project_id PROJECT_ID]
87+
[--registry_id REGISTRY_ID] [--topic_id TOPIC_ID]
88+
[--bucket_name BUCKET_NAME] [--data_path DATA_PATH]
89+
[--service_account_email SERVICE_ACCOUNT_EMAIL]
90+
[--device_access_token DEVICE_ACCESS_TOKEN]
91+
[--command_to_be_sent_to_device COMMAND_TO_BE_SENT_TO_DEVICE]
92+
{generate-access-token,publish-pubsub-message,send-command-to-iot-device,download-cloud-storage-file,exchange-device-token-for-service-account-token}
93+
...
94+
95+
This sample app demonstrates the capabilites of Google Cloud IoT Core device federated authentication feature.
96+
Devices authenticated to Cloud IoT Core can use the [Token Service](https://cloud.google.com/iot/alpha/docs/reference/cloudiottoken/rest) federated authentication to request [OAuth 2.0 access tokens](https://developers.google.com/identity/protocols/oauth2) in exchange for their [Cloud IoT Core JWTs](https://cloud.google.com/iot/docs/how-tos/credentials/jwts).
97+
The OAuth 2.0 credentials can be used to call different [Google Cloud APIs](https://developers.google.com/identity/protocols/oauth2/scopes) with fine-grained permissions and access control using [Workload Identity Federation](https://cloud.google.com/iam/docs/workload-identity-federation).
98+
For more information, see https://cloud.google.com/iot/alpha/docs/how-tos/federated_auth
99+
100+
Usage example:
101+
102+
python accesstoken.py \
103+
--project_id=my-project-id \
104+
--cloud_region=us-central1 \
105+
--registry_id=my-registry-id \
106+
--device_id=my-device-id \
107+
--private_key_file=./resources/rsa_private.pem \
108+
--scope=https://www.googleapis.com/auth/cloud-platform \
109+
--algorithm=RS256 \
110+
generate-access-token
111+
112+
positional arguments:
113+
{generate-access-token,publish-pubsub-message,send-command-to-iot-device,download-cloud-storage-file,exchange-device-token-for-service-account-token}
114+
generate-access-token
115+
Generates OAuth 2.0 Google Access Token.
116+
publish-pubsub-message
117+
Publishes a message to Cloud Pub/Sub topic.
118+
send-command-to-iot-device
119+
Sends a command to an IoT device.
120+
download-cloud-storage-file
121+
Downloads a file from Cloud Storage bucket.
122+
exchange-device-token-for-service-account-token
123+
Exchanges device access token to service account
124+
access token.
125+
126+
optional arguments:
127+
-h, --help show this help message and exit
128+
--algorithm {RS256,ES256}
129+
Encryption algorithm used to generate the device JWT.
130+
--private_key_file PRIVATE_KEY_FILE
131+
Path to private key file.
132+
--cloud_region CLOUD_REGION
133+
GCP cloud region.
134+
--device_id DEVICE_ID
135+
Device ID.
136+
--scope SCOPE Scope for OAuth 2.0 access token. Space delimited
137+
strings. See the full list of scopes at: https://devel
138+
opers.google.com/identity/protocols/oauth2/scopes
139+
--project_id PROJECT_ID
140+
GCP cloud project name.
141+
--registry_id REGISTRY_ID
142+
Registry ID.
143+
--topic_id TOPIC_ID Cloud Pub/Sub topic ID.
144+
--bucket_name BUCKET_NAME
145+
Cloud Storage bucket name.
146+
--data_path DATA_PATH
147+
Path to file to be uploaded.
148+
--service_account_email SERVICE_ACCOUNT_EMAIL
149+
Service account email to exchange device access token
150+
to service account token.
151+
--device_access_token DEVICE_ACCESS_TOKEN
152+
Device access token to exchange for service account
153+
access token.
154+
--command_to_be_sent_to_device COMMAND_TO_BE_SENT_TO_DEVICE
155+
Command to be sent to the IoT device.
156+
157+
158+
159+
160+
161+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Cloud IoT Token Api
5+
short_name: Cloud IoT Token Api
6+
url: https://cloud.google.com/iot/docs
7+
description: >
8+
# Google Cloud IoT Core Python Device Federated Authentication example
9+
This sample app demonstrates the capabilites of Google Cloud IoT Core device federated authentication feature.
10+
Devices authenticated to Cloud IoT Core can use the [Token Service](https://cloud.google.com/iot/alpha/docs/reference/cloudiottoken/rest) federated authentication to request [OAuth 2.0 access tokens](https://developers.google.com/identity/protocols/oauth2) in exchange for their [Cloud IoT Core JWTs](https://cloud.google.com/iot/docs/how-tos/credentials/jwts).
11+
The OAuth 2.0 credentials can be used to call different [Google Cloud APIs](https://developers.google.com/identity/protocols/oauth2/scopes) with fine-grained permissions and access control using [Workload Identity Federation](https://cloud.google.com/iam/docs/workload-identity-federation).
12+
For more information, see https://cloud.google.com/iot/alpha/docs/how-tos/federated_auth
13+
14+
setup:
15+
- auth
16+
- install_deps
17+
18+
samples:
19+
- name: accesstoken
20+
file: accesstoken.py
21+
show_help: True
22+
23+
cloud_client_library: false
24+
25+
folder: iot/api-client/accesstoken_example

0 commit comments

Comments
 (0)