> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 将 Python 连接到 Insights

> 使用 Azure Kusto Python SDK 从 Python 和 Jupyter Notebooks 查询旧版 PlayFab Insights，通过 Microsoft Entra ID 身份验证访问你的游戏数据。

# 教程：将 Python 连接到 Insights

本指南可帮助你开始将 Python 与 Insights 结合使用。它使用 Azure Kusto Python SDK。连接后，你可以使用 Python 查询你的游戏数据，并从 Jupyter Notebooks 使用该库。要了解可与 Insights 连接的其他工具，请参阅[将外部工具连接到 Insights](/services/playfab/data-analytics/legacy/connectivity)。

<Note>
  PlayFab Insights Management 已于 2026 年 3 月 31 日退役。我们建议使用 [Azure Data Explorer (ADX) 连接](/services/playfab/data-analytics/export-data/data-connection-adx)以便今后管理你的性能和成本。如果你的标题仍在使用 **Insights**，请继续查看本文以了解实现详情。有关更多信息，请参阅 [PlayFab Digest：3 月功能更新](https://developer.microsoft.com/en-us/games/articles/2026/04/playfab-digest-march-feature-updates/)。
</Note>

## 先决条件

### 使用 Azure AD 认证的 PlayFab 账户

你需要一个 PlayFab 账户或用户，其身份验证提供商设置为 Microsoft。Microsoft 身份验证提供商使用 Azure Active Directory (Azure AD) 进行身份验证，这是使用 Azure 服务所必需的。有关创建 Azure AD 认证账户或用户的说明，请参阅 [Game Manager 的 Azure Active Directory 身份验证](/services/playfab/identity/dev-identity/authentication/aad-authentication)。

要验证账户或用户是否设置为使用 Microsoft 身份验证提供商：

* 访问 PlayFab [登录页面](https://developer.playfab.com)。
* 选择 **Sign in with Microsoft** 以访问你的 PlayFab 账户。

如果你可以登录，则该账户已设置为使用 Microsoft 身份验证提供商。

### Insights 的 Game Manager 权限

你需要为你的账户分配启用以下 Game Manager 权限的[用户角色](/services/playfab/identity/dev-identity/permissions/playfab-user-roles)：

* 管理员状态。
* 访问 Explorer 选项卡和关联数据。
* 对 Analytics 数据的读写访问。

你可以创建新的用户角色，也可以向现有角色添加这些权限。

### 其他先决条件

* [创建 Azure Active Directory (AAD) 应用程序并将其连接到你的标题数据库](/services/playfab/data-analytics/legacy/connectivity/creating-AAD-app-for-insights)

## 安装 Python 包

1. 使用 [pip](https://pypi.org/project/pip/) 安装以下 Python 包：
   * azure-kusto-data
   * azure-kusto-ingest
   * adal
2. 下面是一个入门示例脚本。

```python theme={null}
from azure.kusto.data.exceptions import KustoServiceError
from azure.kusto.data.request import KustoClient, KustoConnectionStringBuilder, ClientRequestProperties

from msal import ConfidentialClientApplication

cluster = "https://insights.playfab.com"

# These parameters are taken from your Azure app
client_id = "<Azure app client id>"
client_secret = "<Azure app client secret>" 
tenant = "<Azure app tenant id>"

authority_url = "https://login.microsoftonline.com/" + tenant

client_instance = ConfidentialClientApplication(
  client_id=client_id,
  client_credential=client_secret,
  authority=authority_url,
)

# Acquire a token from AAD to pass to PlayFab
_scopes = ["https://help.kusto.windows.net"]
token_response = client_instance.acquire_token_for_client(scopes=_scopes)

token = None
if token_response:
    if token_response['access_token']:
        token = token_response['access_token']

kcsb = KustoConnectionStringBuilder.with_aad_application_token_authentication(cluster, token)
client = KustoClient(kcsb)

db = "<title id>"
query = "['events.all'] | count"

# Force Kusto to use the v1 query endpoint
client._query_endpoint = cluster + "/v1/rest/query"

crp = ClientRequestProperties()
crp.application = "KustoPythonSDK"
response = client.execute(db, query)

# Response processing
print(response)
```

## 其他资源

* [Azure Kusto Python SDK 文档](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/api/python/kusto-python-client-library)
* 要了解连接到 Insights 的其他工具，请参阅[将外部工具连接到 Insights](/services/playfab/data-analytics/legacy/connectivity)


## Related topics

- [将外部工具与 Insights 连接](/zh-CN/services/playfab/data-analytics/legacy/connectivity/index.md)
- [将 Grafana 连接到 Insights](/zh-CN/services/playfab/data-analytics/legacy/connectivity/connecting-grafana-to-insights.md)
- [将 Kusto C# SDK 连接到 Insights](/zh-CN/services/playfab/data-analytics/legacy/connectivity/connecting-kusto-csharp-to-insights.md)
- [将 Kusto Explorer 连接到 Insights](/zh-CN/services/playfab/data-analytics/legacy/connectivity/connecting-kusto-explorer-to-insights.md)
- [将 Power BI 连接到 Insights](/zh-CN/services/playfab/data-analytics/legacy/connectivity/connecting-power-bi-to-insights.md)
