教程: 将 Python 连接到 Insights
本指南帮助你开始将 Python 与 Insights 一同使用。 它使用 Azure Kusto Python SDK。 连接后,可以使用 Python 查询游戏数据,而且可以使用 Jupyter Notebooks 中的库。 若要详细了解可与 Insights 连接的其他工具,请参阅 将外部工具连接到 Insights。
注意
PlayFab Insights 管理已于 2023 年 12 月 11 日起弃用。 我们建议使用 Azure 数据资源管理器 (ADX) 连接 来管理未来性能和成本。 如果你的游戏仍在使用 Insights,请继续查看本文以了解实施详细信息。 有关详细信息,请参阅 Insights 弃用博客。
先决条件
使用 Azure AD 进行身份验证的 PlayFab 帐户
需要一个身份验证提供程序设置为 Microsoft 的 PlayFab 帐户或用户。 Microsoft 身份验证提供程序使用 Azure Active Directory (Azure AD) 进行身份验证,这是使用 Azure 服务所必需的。 有关创建经过 Azure AD 身份验证的帐户或用户的说明,请参阅 Game Manager 的 Azure Active Directory 身份验证。
要验证帐户或用户设置为使用 Microsoft 身份验证提供程序,请:
- 访问 PlayFab 登录页面。
- 选择“使用 Microsoft 登录”以访问 PlayFab 帐户。
如果可以登录,则该帐户设置为使用 Microsoft 身份验证提供程序。
Insights 的 Game Manager 权限
需要向帐户分配已启用以下 Game Manager 权限的 用户角色:
- 管理员状态。
- 访问资源管理器选项卡和关联的数据。
- 对分析数据进行读取和写入访问。
可创建新的用户角色或向现有角色添加这些权限。
其他先决条件
安装 Python 程序包
使用 pip安装以下 python 程序包:
- azure-kusto-data
- azure-kusto-ingest
- adal
下面是一个入门的示例脚本。
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 文档
- 要了解连接到见解的其他工具,请参阅 将外部工具连接到见解