你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Microsoft Entra 身份验证进行 Application Insights API 访问

可以使用 Azure Monitor Application Insights 终结点 https://api.applicationinsights.io 提交查询请求。 若要访问终结点,必须通过 Microsoft Entra ID 进行身份验证。

设置身份验证

若要访问 API,请向 Microsoft Entra ID 注册客户端应用并请求令牌。

  1. 在 Microsoft Entra ID 中注册应用

  2. 在应用的概述页面中,选择“API 权限”。

  3. 选择“添加权限”。

  4. 在“我的组织使用的 API”选项卡上,搜索“Application Insights”,然后从列表中选择“Application Insights API”。

  5. 选择“委托的权限”。

  6. 选中“Data.Read”复选框。

  7. 选择“添加权限”。

现在,应用已注册完成并有权使用 API,请授予你的应用对 Application Insights 资源的访问权限。

  1. Application Insights 资源概述页中,选择“访问控制 (IAM)”。

  2. 选择“添加角色分配”。

  3. 选择“读取者”角色,然后选择“成员”。

  4. 在“成员”选项卡上,选择“选择成员”。

  5. 在“选择”框中,输入应用的名称。

  6. 选择应用,然后选择“选择”。

  7. 选择“查看 + 分配”。

  8. 在完成 Active Directory 设置和权限事项后,请请求授权令牌。

注意

在此示例中,我们应用了“读取者”角色。 此角色是许多内置角色之一,可能包含超过所需的权限。 可以创建更精细的角色和权限。

请求获取授权令牌

在开始之前,请确保你拥有成功进行请求所需的所有值。 所有请求都需要:

  • Microsoft Entra 租户 ID。
  • App Insights 应用 ID - 如果当前使用的是 API 密钥,则这是相同的应用 ID。
  • 应用的 Microsoft Entra 客户端 ID。
  • 应用的 Microsoft Entra 客户端密码。

Application Insights API 通过三种不同的 Microsoft Entra ID OAuth2 流支持 Microsoft Entra 身份验证:

  • 客户端凭据
  • 授权代码
  • 隐式

客户端凭据流

在客户端凭据流中,令牌用于 Application Insights 终结点。 使用上述在 Microsoft Entra ID 中注册应用的步骤中为应用提供的凭据,发出单个请求来接收令牌。

使用 https://api.applicationinsights.io 终结点。

客户端凭据令牌 URL(POST 请求)

    POST /<your-tenant-id>/oauth2/token
    Host: https://login.microsoftonline.com
    Content-Type: application/x-www-form-urlencoded
    
    grant_type=client_credentials
    &client_id=<app-client-id>
    &resource=https://api.applicationinsights.io
    &client_secret=<app-client-secret>

成功的请求在响应中接收访问令牌:

    {
        token_type": "Bearer",
        "expires_in": "86399",
        "ext_expires_in": "86399",
        "access_token": ""eyJ0eXAiOiJKV1QiLCJ.....Ax"
    }

在对 Application Insights 终结点的请求中使用令牌:

    POST /v1/apps/yous_app_id/query?timespan=P1D
    Host: https://api.applicationinsights.io
    Content-Type: application/json
    Authorization: Bearer <your access token>

    Body:
    {
    "query": "requests | take 10"
    }

示例响应:

  "tables": [
    {
      "name": "PrimaryResult",
      "columns": [
        {
          "name": "timestamp",
          "type": "datetime"
        },
        {
          "name": "id",
          "type": "string"
        },
        {
          "name": "source",
          "type": "string"
        },
        {
          "name": "name",
          "type": "string"
        },
        {
          "name": "url",
          "type": "string"
        },
        {
          "name": "success",
          "type": "string"
        },
        {
          "name": "resultCode",
          "type": "string"
        },
        {
          "name": "duration",
          "type": "real"
        },
        {
          "name": "performanceBucket",
          "type": "string"
        },
        {
          "name": "customDimensions",
          "type": "dynamic"
        },
        {
          "name": "customMeasurements",
          "type": "dynamic"
        },
        {
          "name": "operation_Name",
          "type": "string"
        },
        {
          "name": "operation_Id",
          "type": "string"
        },
        {
          "name": "operation_ParentId",
          "type": "string"
        },
        {
          "name": "operation_SyntheticSource",
          "type": "string"
        },
        {
          "name": "session_Id",
          "type": "string"
        },
        {
          "name": "user_Id",
          "type": "string"
        },
        {
          "name": "user_AuthenticatedId",
          "type": "string"
        },
        {
          "name": "user_AccountId",
          "type": "string"
        },
        {
          "name": "application_Version",
          "type": "string"
        },
        {
          "name": "client_Type",
          "type": "string"
        },
        {
          "name": "client_Model",
          "type": "string"
        },
        {
          "name": "client_OS",
          "type": "string"
        },
        {
          "name": "client_IP",
          "type": "string"
        },
        {
          "name": "client_City",
          "type": "string"
        },
        {
          "name": "client_StateOrProvince",
          "type": "string"
        },
        {
          "name": "client_CountryOrRegion",
          "type": "string"
        },
        {
          "name": "client_Browser",
          "type": "string"
        },
        {
          "name": "cloud_RoleName",
          "type": "string"
        },
        {
          "name": "cloud_RoleInstance",
          "type": "string"
        },
        {
          "name": "appId",
          "type": "string"
        },
        {
          "name": "appName",
          "type": "string"
        },
        {
          "name": "iKey",
          "type": "string"
        },
        {
          "name": "sdkVersion",
          "type": "string"
        },
        {
          "name": "itemId",
          "type": "string"
        },
        {
          "name": "itemType",
          "type": "string"
        },
        {
          "name": "itemCount",
          "type": "int"
        }
      ],
      "rows": [
        [
          "2018-02-01T17:33:09.788Z",
          "|0qRud6jz3k0=.c32c2659_",
          null,
          "GET Reports/Index",
          "http://fabrikamfiberapp.azurewebsites.net/Reports",
          "True",
          "200",
          "3.3833",
          "<250ms",
          "{\"_MS.ProcessedByMetricExtractors\":\"(Name:'Requests', Ver:'1.0')\"}",
          null,
          "GET Reports/Index",
          "0qRud6jz3k0=",
          "0qRud6jz3k0=",
          "Application Insights Availability Monitoring",
          "9fc6738d-7e26-44f0-b88e-6fae8ccb6b26",
          "us-va-ash-azr_9fc6738d-7e26-44f0-b88e-6fae8ccb6b26",
          null,
          null,
          "AutoGen_49c3aea0-4641-4675-93b5-55f7a62d22d3",
          "PC",
          null,
          null,
          "52.168.8.0",
          "Boydton",
          "Virginia",
          "United States",
          null,
          "fabrikamfiberapp",
          "RD00155D5053D1",
          "cf58dcfd-0683-487c-bc84-048789bca8e5",
          "fabrikamprod",
          "5a2e4e0c-e136-4a15-9824-90ba859b0a89",
          "web:2.5.0-33031",
          "051ad4ef-0776-11e8-ac6e-e30599af6943",
          "request",
          "1"
        ],
        [
          "2018-02-01T17:33:15.786Z",
          "|x/Ysh+M1TfU=.c32c265a_",
          null,
          "GET Home/Index",
          "http://fabrikamfiberapp.azurewebsites.net/",
          "True",
          "200",
          "716.2912",
          "500ms-1sec",
          "{\"_MS.ProcessedByMetricExtractors\":\"(Name:'Requests', Ver:'1.0')\"}",
          null,
          "GET Home/Index",
          "x/Ysh+M1TfU=",
          "x/Ysh+M1TfU=",
          "Application Insights Availability Monitoring",
          "58b15be6-d1e6-4d89-9919-52f63b840913",
          "emea-se-sto-edge_58b15be6-d1e6-4d89-9919-52f63b840913",
          null,
          null,
          "AutoGen_49c3aea0-4641-4675-93b5-55f7a62d22d3",
          "PC",
          null,
          null,
          "51.141.32.0",
          "Cardiff",
          "Cardiff",
          "United Kingdom",
          null,
          "fabrikamfiberapp",
          "RD00155D5053D1",
          "cf58dcfd-0683-487c-bc84-048789bca8e5",
          "fabrikamprod",
          "5a2e4e0c-e136-4a15-9824-90ba859b0a89",
          "web:2.5.0-33031",
          "051ad4f0-0776-11e8-ac6e-e30599af6943",
          "request",
          "1"
        ]
      ]
    }
  ]
}

授权代码流

支持的主要 OAuth2 流是通过授权代码实现的。 此方法需要两个 HTTP 请求来获取用于调用 Azure Monitor Application Insights API 的令牌。 有两个 URL,每个请求对应一个终结点。 后续章节将介绍其格式。

授权代码 URL(GET 请求)

    GET https://login.microsoftonline.com/YOUR_Azure AD_TENANT/oauth2/authorize?
    client_id=<app-client-id>
    &response_type=code
    &redirect_uri=<app-redirect-uri>
    &resource=https://api.applicationinsights.io

向授权 URL 发出请求时,client_id 是 Microsoft Entra 应用的应用程序 ID(从应用的属性菜单中复制)。 redirect_uri 是来自同一 Microsoft Entra 应用的主页/登录 URL。 请求成功时,此终结点会将你重定向到你在注册时提供的登录页,并将授权代码追加到 URL。 请参阅以下示例:

    http://<app-client-id>/?code=AUTHORIZATION_CODE&session_state=STATE_GUID

此时,你已获得授权代码,现在需要使用该代码来请求访问令牌。

授权代码令牌 URL(POST 请求)

    POST /YOUR_Azure AD_TENANT/oauth2/token HTTP/1.1
    Host: https://login.microsoftonline.com
    Content-Type: application/x-www-form-urlencoded
    
    grant_type=authorization_code
    &client_id=<app client id>
    &code=<auth code fom GET request>
    &redirect_uri=<app-client-id>
    &resource=https://api.applicationinsights.io
    &client_secret=<app-client-secret>

所有值都与以前相同,只是增加了一些值。 该授权代码与在成功重定向后在上一个请求中收到的代码相同。 该代码与从 Microsoft Entra 应用获取的密钥结合在一起。 如果未保存该密钥,可将其删除,并从 Microsoft Entra 应用菜单的“密钥”选项卡中创建一个新密钥。 响应是包含具有以下架构的令牌的 JSON 字符串。 为令牌值指明了类型。

响应示例:

    {
        "access_token": "eyJ0eXAiOiJKV1QiLCJ.....Ax",
        "expires_in": "3600",
        "ext_expires_in": "1503641912",
        "id_token": "not_needed_for_app_insights",
        "not_before": "1503638012",
        "refresh_token": "eyJ0esdfiJKV1ljhgYF.....Az",
        "resource": "https://api.applicationinsights.io",
        "scope": "Data.Read",
        "token_type": "bearer"
    }

此响应的访问令牌部分是在 Authorization: Bearer 标头中提供给 Application Insights API 的内容。 将来如果令牌过时,还可以使用刷新令牌来获取新的 access_token 和 refresh_token。 对于此请求,格式和终结点为:

    POST /YOUR_AAD_TENANT/oauth2/token HTTP/1.1
    Host: https://login.microsoftonline.com
    Content-Type: application/x-www-form-urlencoded
    
    client_id=<app-client-id>
    &refresh_token=<refresh-token>
    &grant_type=refresh_token
    &resource=https://api.applicationinsights.io
    &client_secret=<app-client-secret>

响应示例:

    {
      "token_type": "Bearer",
      "expires_in": "3600",
      "expires_on": "1460404526",
      "resource": "https://api.applicationinsights.io",
      "access_token": "eyJ0eXAiOiJKV1QiLCJ.....Ax",
      "refresh_token": "eyJ0esdfiJKV1ljhgYF.....Az"
    }

隐式代码流

Application Insights API 还支持 OAuth2 隐式流。 对于此流,只需要单个请求,但无法获取刷新令牌。

隐式代码授权 URL

    GET https://login.microsoftonline.com/YOUR_AAD_TENANT/oauth2/authorize?
    client_id=<app-client-id>
    &response_type=token
    &redirect_uri=<app-redirect-uri>
    &resource=https://api.applicationinsights.io

成功的请求将使用 URL 中的令牌生成到你的重定向 URI 的重定向:

    http://YOUR_REDIRECT_URI/#access_token=YOUR_ACCESS_TOKEN&token_type=Bearer&expires_in=3600&session_state=STATE_GUID

此 access_token 在传递给 Application Insights API 以授权请求时可用作 Authorization: Bearer 标头值。