连接到媒体服务 v3 API - PythonConnect to Media Services v3 API - Python

媒体服务徽标 v3media services logo v3


本文介绍如何使用服务主体登录方法连接到 Azure 媒体服务 v3 Python SDK。This article shows you how to connect to the Azure Media Services v3 Python SDK using the service principal sign in method.

必备条件Prerequisites

  • python.org 下载 PythonDownload Python from python.org
  • 请确保设置 PATH 环境变量Make sure to set the PATH environment variable
  • 创建媒体服务帐户Create a Media Services account. 请务必记住资源组名称和媒体服务帐户名称。Be sure to remember the resource group name and the Media Services account name.
  • 按照访问 API 主题中的步骤进行操作。Follow the steps in the Access APIs topic. 记下订阅 ID、应用程序 ID(客户端 ID)、身份验证密钥(机密)和租户 ID,以便在后面的步骤中使用。Record the subscription ID, application ID (client ID), the authentication key (secret), and the tenant ID that you need in the later step.

重要

查看命名约定Review naming conventions.

安装模块Install the modules

若要通过 Python 使用 Azure 媒体服务,需要安装以下模块。To work with Azure Media Services using Python, you need to install these modules.

  • azure-mgmt-resource 模块,其中包括用于 Active Directory 的 Azure 模块。The azure-mgmt-resource module, which includes Azure modules for Active Directory.
  • azure-mgmt-media 模块,其中包括媒体服务实体。The azure-mgmt-media module, which includes the Media Services entities.

打开命令行工具并使用以下命令安装这些模块。Open a command-line tool and use the following commands to install the modules.

pip3 install azure-mgmt-resource
pip3 install azure-mgmt-media==1.1.1

连接到 Python 客户端Connect to the Python client

  1. 创建扩展名为 .py 的文件Create a file with a .py extension

  2. 在偏好的编辑器中打开该文件Open the file in your favorite editor

  3. 将下面的代码添加到该文件。Add the code that follows to the file. 此代码将导入所需的模块,并创建连接到媒体服务所需的 Active Directory 凭据对象。The code imports the required modules and creates the Active Directory credentials object you need to connect to Media Services.

    将变量的值设置为从访问 API 中获得的值Set the variables' values to the values you got from Access APIs

    import adal
    from msrestazure.azure_active_directory import AdalAuthentication
    from msrestazure.azure_cloud import AZURE_CHINA_CLOUD
    from azure.mgmt.media import AzureMediaServices
    from azure.mgmt.media.models import MediaService
    
    RESOURCE = 'https://management.core.chinacloudapi.cn/'
    # Tenant ID for your Azure Subscription
    TENANT_ID = '00000000-0000-0000-000000000000'
    # Your Service Principal App ID
    CLIENT = '00000000-0000-0000-000000000000'
    # Your Service Principal Password
    KEY = '00000000-0000-0000-000000000000'
    # Your Azure Subscription ID
    SUBSCRIPTION_ID = '00000000-0000-0000-000000000000'
    # Your Azure Media Service (AMS) Account Name
    ACCOUNT_NAME = 'amsv3account'
    # Your Resource Group Name
    RESOUCE_GROUP_NAME = 'AMSv3ResourceGroup'
    
    LOGIN_ENDPOINT = AZURE_CHINA_CLOUD.endpoints.active_directory
    RESOURCE = AZURE_CHINA_CLOUD.endpoints.active_directory_resource_id
    
    context = adal.AuthenticationContext(LOGIN_ENDPOINT + '/' + TENANT_ID)
    credentials = AdalAuthentication(
        context.acquire_token_with_client_credentials,
        RESOURCE,
        CLIENT,
        KEY
    )
    
    # The AMS Client
    # You can now use this object to perform different operations to your AMS account.
    client = AzureMediaServices(credentials, SUBSCRIPTION_ID, base_url="https://management.chinacloudapi.cn")
    
    print("signed in")
    
    # Now that you are authenticated, you can manipulate the entities.
    # For example, list assets in your AMS account
    print (client.assets.list(RESOUCE_GROUP_NAME, ACCOUNT_NAME).get(0))
    
  4. 运行该文件Run the file

后续步骤Next steps