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 のセットアップとアクセス許可が完了したら、認可トークンを要求します。

注意

この例では、閲覧者ロールを適用しました。 このロールは多くの組み込みロールの 1 つであり、必要以上のアクセス許可が含まれる場合があります。 より詳細なロールとアクセス許可を作成できます。

承認トークンを要求する

開始する前に、要求を正常に行うために必要なすべての値があることを確認してください。 すべての要求には、次が必要です。

  • Microsoft Entra テナント ID。
  • App Insights アプリ ID - API キーを現在使用している場合、これは同じアプリ ID です。
  • アプリの Microsoft Entra クライアント ID。
  • アプリの Microsoft Entra クライアント シークレット。

Application Insights API では、3 つの異なる Microsoft Entra ID OAuth2 フローで Microsoft Entra 認証がサポートされています。

  • クライアントの資格情報
  • Authorization code (承認コード)
  • 暗黙

クライアントの資格情報フロー

クライアント資格情報フローでは、トークンは Application Insights エンドポイントで使用されます。 前のステップでアプリを Microsoft Entra ID に登録したときにアプリに提供された資格情報を使い、1 回要求を行ってトークンを受け取ります。

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 フローは、承認コードを使用して行われます。 この方法では、Azure Monitor Application Insights API の呼び出しに使用するトークンを取得するために 2 つの HTTP 要求が必要です。 2 つの URL (要求ごとに 1 つのエンドポイント) があります。 これらの形式については、次のセクションで説明します。

認可コード 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 の暗黙的フローがサポートされています。 このフローでは、1 つの要求のみが必要ですが、更新トークンは取得できません。

暗黙的コード認可 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 ヘッダー値として使用できます。