警告
下列影片與程式碼會將使用者名稱、密碼及帳戶名稱提交至 /rest/authentication/login/,屬於舊版帳戶的舊式 REST 流程,不適用於 Cloud One 帳戶。Cloud One 帳戶應建立 Cloud One API key,使用帳戶資料所在區域的 Workload Security API 端點,並在 HTTP 標頭中提供 Authorization: ApiKey <API key>。請勿將此範例視為現行建議作法。
舊版帳戶的 Workload Security API 程式碼
危險
範例中的 verify=False 會停用 TLS 憑證驗證,僅為歷史教材內容;正式環境不得沿用此設定。
from __future__ import print_function
import requests
import json
# Set the variables
url = "https://cloudone.trendmicro.com:443"
data = {"dsCredentials":{"userName":"your username","password":"your password","tenantName":"your tenant name"}}
headers = {'Content-type': 'application/json'}
#Log in, get the session ID, and set the cookies for the post request
ses = requests.session()
loginurl = url + "/rest/authentication/login/"
res = ses.post(loginurl,data=json.dumps(data), headers=headers, verify=False)
cookies = {'sID':res.text}
#Add the Azure Account
data2 = {"createCloudAccountRequest":{"cloudAccountElement":{"subscriptionId":"your subscription Id","cloudType":"AZURE_ARM","name":"Automation Azure Account","azureAdTenantId":"Your azure AD tenant ID","azureAdApplicationId":"Your azure AD Application ID","azureAdApplicationPassword":"Your Secret Key"},"sessionId":res.text}}
res2 = ses.post(url + "/rest/cloudaccounts/", data=json.dumps(data2), headers=headers, verify=False)
print(res2.text)
#Log out
logouturl = url + "/rest/authentication/logout/?sID={" + res.text + "}"
print(logouturl)
res3 = ses.delete(logouturl,verify=False)
print(res3.text)