設定 Devices API
本頁面說明如何設定 Cloud Identity Devices API。您可以使用 Devices API,透過程式為管理員佈建資源,例如管理 Google 網路論壇。
啟用 API 並設定憑證
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Identity API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Identity API.
如要建立服務帳戶,請按照下列步驟操作:
在 Google Cloud 控制台中前往「IAM 服務帳戶」頁面:
按一下
「建立服務帳戶」。在「Service account details」(服務帳戶詳細資料) 下方,輸入服務帳戶的名稱、ID 和說明,然後按一下「Create and continue」(建立並繼續)。
選用:在「將專案存取權授予這個服務帳戶」下方,選取要授予服務帳戶的 IAM 角色。
按一下「繼續」。
選用:在「Grant users access to this service account」(將這個服務帳戶的存取權授予使用者) 下方,新增可使用及管理服務帳戶的使用者或群組。
按一下 [完成]。
如要讓服務帳戶使用全網域委派功能存取 Devices API,請按照「為服務帳戶設定全網域委派」一文的說明操作。
如要建立及下載服務帳戶金鑰,請按照下列步驟操作:
- 按一下您建立的服務帳戶電子郵件地址。
- 按一下「Keys」(金鑰) 分頁標籤。
- 在「Add key」下拉式選單中,選取「Create new key」。
按一下 [建立]。
系統會產生並下載 JSON 格式的憑證檔案,其中包含新的公開和私密金鑰組。檔案中只含有金鑰的唯一副本。您必須負責妥善保管。如果您遺失金鑰組,就必須產生新的金鑰組。
SERVICE_ACCOUNT_CREDENTIAL_FILE
:您先前在本文件中建立的服務帳戶金鑰檔案USER
:服務帳戶模擬的使用者SERVICE_ACCOUNT_CREDENTIAL_FILE
:您先前在本文件中建立的服務帳戶金鑰檔案USER
:服務帳戶模擬的使用者
使用全網域委派服務帳戶設定 API 存取權
本節說明如何建立及使用服務帳戶來存取 Google Workspace 資源。系統不支援使用服務帳戶直接驗證 Devices API,因此您必須使用這個方法。
建立服務帳戶並設定全網域委派功能
如要建立服務帳戶並下載服務帳戶金鑰,請按照下列步驟操作:
查看記錄項目
請注意,查看記錄項目時,稽核記錄會顯示任何服務帳戶動作都是由使用者啟動。這是因為全網域委派功能會允許服務帳戶模擬管理員使用者。
初始化憑證
在程式碼中初始化憑證時,請對憑證呼叫 with_subject()
,指定服務帳戶的電子郵件地址。例如:
Python
credentials = service_account.Credentials.from_service_account_file(
'SERVICE_ACCOUNT_CREDENTIAL_FILE',
scopes=SCOPES).with_subject(USER
)
更改下列內容:
例項化用戶端
以下範例說明如何使用服務帳戶憑證例項化用戶端。
Python
from google.oauth2 import service_account
import googleapiclient.discovery
SCOPES = ['https://www.googleapis.com/auth/cloud-identity.devices']
def create_service():
credentials = service_account.Credentials.from_service_account_file(
'SERVICE_ACCOUNT_CREDENTIAL_FILE',
scopes=SCOPES
)
delegated_credentials = credentials.with_subject('USER')
service_name = 'cloudidentity'
api_version = 'v1'
service = googleapiclient.discovery.build(
service_name,
api_version,
credentials=delegated_credentials)
return service
更改下列內容:
您現在可以開始呼叫 Devices API。
如要改為以使用者身分進行驗證,請將服務帳戶中的 credential
物件替換為您在「取得 OAuth 2.0 權杖」一文中取得的 credential
。
安裝 Python 用戶端程式庫
如要安裝 Python 用戶端程式庫,請執行下列指令:
pip install --upgrade google-api-python-client google-auth \
google-auth-oauthlib google-auth-httplib2
如要進一步瞭解如何設定 Python 開發環境,請參閱 Python 開發環境設定指南。