天蓝色活动日志的python脚本

时间:2019-03-17 22:14:19

标签: python azure

from azure.monitor import MonitorClient
#from azure.mgmt.monitor import MonitorMgmtClient
from azure.mgmt.monitor import MonitorManagementClient
from azure.common.credentials import UserPassCredentials
import datetime

# Replace this with your subscription id
subscription_id = '************'

# See above for details on creating different types of AAD credentials
credentials = UserPassCredentials(
'****',  # Your user
'****',      # Your password
)

client = MonitorClient(
  credentials,
  subscription_id
 )

monitor_mgmt_client = MonitorManagementClient(
  credentials,
  subscription_id
)

运行此代码后,其给出错误: 引发错误 msrest.exceptions.AuthenticationError:,InvalidClientIdError:(invalid_request)AADSTS900144:请求正文必须包含以下参数:'client_id'

1 个答案:

答案 0 :(得分:1)

似乎您不应该再使用UserPassCredentials,因为它已被弃用。

查看此link

  

在早期版本的SDK中,ADAL尚不可用,我们提供了UserPassCredentials类。该方法已被弃用,不应再使用。

对于使用令牌凭证进行身份验证,您可以尝试以下代码。

from azure.common.credentials import ServicePrincipalCredentials

# Tenant ID for your Azure Subscription
TENANT_ID = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'

# Your Service Principal App ID
CLIENT = 'a2ab11af-01aa-4759-8345-7803287dbd39'

# Your Service Principal Password
KEY = 'password'

credentials = ServicePrincipalCredentials(
    client_id = CLIENT,
    secret = KEY,
    tenant = TENANT_ID
)

如果您需要更多控制权,建议使用ADAL和SDK ADAL包装器。

import adal
from msrestazure.azure_active_directory import AdalAuthentication
from msrestazure.azure_cloud import AZURE_PUBLIC_CLOUD

# Tenant ID for your Azure Subscription
TENANT_ID = 'ABCDEFGH-1234-1234-1234-ABCDEFGHIJKL'

# Your Service Principal App ID
CLIENT = 'a2ab11af-01aa-4759-8345-7803287dbd39'

# Your Service Principal Password
KEY = 'password'

LOGIN_ENDPOINT = AZURE_PUBLIC_CLOUD.endpoints.active_directory
RESOURCE = AZURE_PUBLIC_CLOUD.endpoints.active_directory_resource_id

context = adal.AuthenticationContext(LOGIN_ENDPOINT + '/' + TENANT_ID)
credentials = AdalAuthentication(
    context.acquire_token_with_client_credentials,
    RESOURCE,
    CLIENT,
    KEY
)

有关更多详细信息,您可以参考以下链接: Authenticate with the Azure Management Libraries for Python

如果您没有AD App,请按照此link进行创建。要获取客户端ID(客户端ID与应用程序ID相同)和密钥,请遵循以下link