> ## 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 搭配 Microsoft Entra ID 驗證，從 Python 和 Jupyter Notebook 查詢舊版 PlayFab Insights 的遊戲資料。

# 教學課程：將 Python 連線至 Insights

本指南可協助您開始搭配 Insights 使用 Python。它使用 Azure Kusto Python SDK。連線之後，您可以使用 Python 查詢遊戲資料，並從 Jupyter Notebook 使用該程式庫。若要深入了解可與 Insights 連線的其他工具，請參閱[將外部工具連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity)。

<Note>
  PlayFab Insights 管理已於 3/31/2026 淘汰。我們建議日後使用 [Azure Data Explorer (ADX) 連線](/zh-TW/services/playfab/data-analytics/export-data/data-connection-adx)來管理您的效能和成本。如果您的遊戲仍在使用 **Insights**，請繼續參閱本文以了解實作詳細資料。如需詳細資訊，請參閱 [PlayFab Digest: March feature updates](https://developer.microsoft.com/en-us/games/articles/2026/04/playfab-digest-march-feature-updates/)。
</Note>

## 必要條件

### 使用 Azure AD 驗證的 PlayFab 帳戶

您需要一個驗證提供者設定為 Microsoft 的 PlayFab 帳戶或使用者。Microsoft 驗證提供者使用 Azure Active Directory (Azure AD) 進行驗證，這是使用 Azure 服務的必要條件。如需建立 Azure AD 驗證帳戶或使用者的指示，請參閱 [Game Manager 的 Azure Active Directory 驗證](/zh-TW/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 權限的[使用者角色](/zh-TW/services/playfab/identity/dev-identity/permissions/playfab-user-roles)：

* 系統管理員狀態。
* 存取 Explorer 索引標籤及相關資料。
* Analytics 資料的讀取和寫入權限。

您可以建立新的使用者角色，或將這些權限新增至現有角色。

### 其他必要條件

* [建立 Azure Active Directory (AAD) 應用程式並將其連線至您的遊戲資料庫](/zh-TW/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](/zh-TW/services/playfab/data-analytics/legacy/connectivity)


## Related topics

- [將外部工具與 Insights 連線](/zh-TW/services/playfab/data-analytics/legacy/connectivity/index.md)
- [將 Grafana 連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity/connecting-grafana-to-insights.md)
- [將 Kusto C# SDK 連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity/connecting-kusto-csharp-to-insights.md)
- [將 Kusto Explorer 連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity/connecting-kusto-explorer-to-insights.md)
- [將 Power BI 連線至 Insights](/zh-TW/services/playfab/data-analytics/legacy/connectivity/connecting-power-bi-to-insights.md)
