APIトークンの追加

最終更新日: 11 年 2024 月 XNUMX 日

LogicMonitor REST API v3 を使用して API トークンを追加できます。 API リクエストを行う前に、自分自身を認証する必要があります。

注: このリソースではすべての POST パラメータはオプションですが、空のペイロードには一連の中括弧 {} を含める必要があります。

URI: 投稿 /setting/admins/{adminID}/apitokens

タイプ説明
adminID整数(必須の) API トークンを追加するユーザーの ID。
type文字列デフォルト値は API token.
note文字列API トークンに関連付けられたメモ。例 - "note「:「John Doe の API トークン」
status整数API トークンが有効かどうかを示します。受け入れられる値は 1 と 2 で、1 は無効を示し、2 は有効を示します。

次の Python スクリプトは、ID 32 のユーザーの API トークンのセットを追加します。

#!/bin/env python
 
import requests
import json
import hashlib
import base64
import time
import hmac
import getpass
 
#Account Info: LogicMonitor recommends to NEVER hardcode the credentials. Instead, retrieve the values from a secure storage.
#Note: The below is provided for illustration purposes only.
AccessId = getpass.getpass("Enter your AccessId: ")
AccessKey = getpass.getpass("Enter your AccessKey: ")
Company = 'apiAccount'  

#Request Info
httpVerb ='POST'
resourcePath = '/setting/admins/32/apitokens'
queryParams =''
data = '{}'

#Construct URL 
url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath + queryParams

#Get current time in milliseconds
epoch = str(int(time.time() * 1000))

#Concatenate Request details
requestVars = httpVerb + epoch + data + resourcePath

#Construct signature
digest = hmac.new(
        AccessKey.encode('utf-8'),
        msg=requestVars.encode('utf-8'),
        digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(digest.encode('utf-8')).decode('utf-8')     

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + str(signature) + ':' + epoch
headers = {'Content-Type':'application/json','Authorization':auth,'X-Version':3}   

# Make request
response = requests.post(url, data=data, headers=headers)
 
# Print status and body of response
print('Response Status:',response.status_code)
print('Response Body:',response.content)
Pythonの3