使用 Microsoft Entra 驗證的 Application Insights API 存取
您可以使用 Azure 監視器 Application Insights 端點 https://api.applicationinsights.io
提交查詢要求。 若要存取該端點,您必須透過 Microsoft Entra ID 進行驗證。
設定驗證
若要存取 API,請使用 Microsoft Entra ID 註冊用戶端應用程式,並要求權杖。
在應用程式的概觀頁面上,選取 [API 權限]。
選取新增權限。
在 [我的組織使用的 API] 索引標籤上搜尋 Application Insights,然後從清單中選取 [Application Insights API]。
選取委派的權限。
選取 [Data.Read] 核取方塊。
選取新增權限。
現在,您的應用程式已註冊並具有使用 API 的權限,請為應用程式授與 Application Insights 資源的存取權。
在 Application Insights 資源概觀頁面中,選取 [存取控制 (IAM)]。
選取 [新增角色指派]。
選取 [讀者] 角色,然後選取 [成員]。
在 [成員] 索引標籤上,選擇 [選取成員]。
在 [選取] 方塊中輸入應用程式的名稱。
選取您的應用程式,然後選擇 [選取]。
選取檢閱+指派。
在完成 Active Directory 設定和權限作業後,要求授權權杖。
注意
在此範例中,我們套用了讀者角色。 此角色是眾多內建角色之一,可能包含比您所需還要多的權限。 您可以建立更細微的角色和權限。
要求授權權杖
開始之前,請確定您具有成功提出要求所需的所有值。 所有要求都需要:
- 您的 Microsoft Entra 租用戶識別碼。
- 您的 App Insights 應用程式識別碼 - 如果您目前使用 API 金鑰,這會是相同的應用程式識別碼。
- 應用程式的 Microsoft Entra 用戶端識別碼。
- 應用程式的 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 監視器 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 應用程式的應用程式識別碼 (複製自應用程式的屬性功能表)。 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
成功的要求會產生重新導向至您的重新導向 URI,並在 URL 中包含權杖:
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
標頭值。