このページでは, 認証APIを使ってアクセストークンを取得し, カメラ一覧および映像再生用のURLを取得するPythonのサンプルプログラムを紹介します。
import requests
import base64
client_id = 'お客様のclientId'
secret_key = 'お客様のsecretKey'
subscription_id = 'お客様のテナントのサブスクリプションID'
token_api_path = 'https://api.amnimo.com/ex/oauth2/token'
camera_api_path = f'https://api.amnimo.com/ex/partitions/{subscription_id}/cameras'
# アクセストークンの取得
def get_access_token():
credentials = f"{client_id}:{secret_key}"
encoded_credentials = base64.b64encode(credentials.encode()).decode()
headers = {
'Authorization': f'Basic {encoded_credentials}',
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {'grant_type': 'client_credentials'}
response = requests.post(token_api_path, headers=headers, data=data)
if response.status_code == 200:
return response.json().get('access_token')
else:
print(f"Failed to obtain access token: {response.status_code}")
return None
# カメラ一覧の取得
def get_camera_list(access_token):
headers = {
'Authorization': f'Bearer {access_token}'
}
response = requests.get(camera_api_path, headers=headers)
if response.status_code == 200:
res_body = response.json()
for camera in res_body['cameras']:
print(f"Name: {camera['displayName']}, Url: {camera['thumbnail']['url']}")
else:
print(f"Failed to retrieve cameras: {response.status_code}")
# メイン処理
def main():
access_token = get_access_token()
if access_token:
get_camera_list(access_token)
if __name__ == "__main__":
main()
import requests
import base64
# 認証情報とエンドポイント
client_id = 'お客様のclientId'
secret_key = 'お客様のsecretKey'
subscription_id = 'お客様のテナントのサブスクリプションID'
camera_id = 'お客様のテナントのカメラID'
token_api_path = 'https://api.amnimo.com/ex/oauth2/token'
hlsurl_api_path = f'https://api.amnimo.com/ex/partitions/{subscription_id}/cameras/{camera_id}/streams/hls/start'
# アクセストークンの取得
def get_access_token():
credentials = f"{client_id}:{secret_key}"
encoded_credentials = base64.b64encode(credentials.encode()).decode()
headers = {
'Authorization': f'Basic {encoded_credentials}',
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {'grant_type': 'client_credentials'}
response = requests.post(token_api_path, headers=headers, data=data)
if response.status_code == 200:
return response.json().get('access_token')
else:
print(f"Failed to obtain access token: {response.status_code}")
return None
# 映像再生URLの取得(ライブ)
def get_streaming_url(access_token):
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
params = {
'dt': 'live'
}
response = requests.get(
hlsurl_api_path,
headers=headers,
params=params
)
if response.status_code == 200:
url = response.json().get('HLSURL')
print(f"Streaming URL: {url}")
else:
print(f"Failed to retrieve streaming URL: {response.status_code}")
# メイン処理
def main():
access_token = get_access_token()
if access_token:
get_streaming_url(access_token)
if __name__ == "__main__":
main()
必要に応じて、client_id、secret_key、subscription_id、camera_idを環境に合わせて変更してください。