> ## 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.

# 来自 title 实体的游戏服务器

> 在 PlayFab Services SDK 中使用 PFAuthenticationGetEntityWithSecretKeyAsync 以 title 实体身份进行身份验证，以便在 Win32 上调用仅限服务器的 API。

# 使用 title 实体访问 PlayFab

与以玩家身份登录并进行 PlayFab 调用的方式类似，你也可以以 title 实体的身份进行身份验证，并通过它进行 PlayFab 调用。Title 实体可以使用某些玩家实体无法使用的函数。这些函数中许多都以 "server" 一词为前缀，一个例子是 ServerAddPlayerTag。

获取 title 实体句柄的初始化步骤与以玩家身份登录相同。有关详细信息，请参阅 [Win32 快速入门](/services/playfab/sdks/c/quickstart-win32) 文章的 "Initialization" 部分。

注意：目前，服务器函数仅在 SDK 的 Win32 版本中可用。

## 对 title 实体进行身份验证

在初始步骤中创建 **PFServiceConfigHandle** 之后，你可以通过调用 [**PFAuthenticationGetEntityWithSecretKeyAsync**](/services/playfab/api-references/c/pfauthentication/functions/pfauthenticationgetentitywithsecretkeyasync) 获取一个 title 实体句柄。获取实体调用成功完成后，它将返回 **S\_OK** 以及一个 **PFEntityHandle**。此过程类似于以玩家身份登录。**PFEntityHandle** 的返回类型相同，只是在此情况下实体句柄表示 title 实体而非玩家实体。我们可以称此实体为游戏服务器。

```cpp theme={null}
    PFAuthenticationGetEntityRequest request{};
    XAsyncBlock async{};

    // Add your own error handling when FAILED(hr) == true
    HRESULT hr = PFAuthenticationGetEntityWithSecretKeyAsync(
        serviceConfigHandle,
        secretKey, // title specific secret key
        &request,
        &async
    );

    hr = XAsyncGetStatus(&async, true); // This is doing a blocking wait for completion, but you can use the XAsyncBlock to set a callback instead for async style usage

    PFEntityHandle entityHandle;
    hr = PFAuthenticationGetEntityWithSecretKeyGetResult(
        &async,
        &entityHandle
    );
```

## 服务调用

创建游戏服务器后，你可以对 PlayFab 后端进行调用。以下是一个为 title 中特定玩家添加玩家标签的示例。

```cpp theme={null}
    XAsyncBlock async{};
    PFSegmentsAddPlayerTagRequest request{};
    addSegmentRequest.PlayFabId = "ABCDEF123456";
    addSegmentRequest.tagName = "tagName";
    HRESULT hr = PFSegmentsServerAddPlayerTagAsync(entityHandle, &request, &async);
    hr = XAsyncGetStatus(&async, true);
```

与 AddPlayerTag 示例类似，游戏服务器使用的许多函数涉及获取或更改玩家数据。此函数要求拥有特定玩家的 PlayFab ID。已连接的客户端可以提供 PlayFab ID，或者服务器可以通过 GetPlayFabIDsFromXboxLiveIDs 等调用直接从 PlayFab 检索它们。

## 参考

[API 参考文档](/services/playfab/api-references/c/pfauthentication/pfauthentication_members)


## Related topics

- [使用 LocalMultiplayerAgent 调试基于进程的游戏服务器](/zh-CN/services/playfab/multiplayer/servers/LocalMultiplayerAgent/run-process-based-gameserver.md)
- [在本地调试游戏服务器以及与 PlayFab 的集成](/zh-CN/services/playfab/multiplayer/servers/locally-debugging-game-servers-and-integration-with-playfab.md)
- [直接连接以调试游戏服务器](/zh-CN/services/playfab/multiplayer/servers/directly-debugging-game-servers.md)
- [游戏服务器配置概述](/zh-CN/services/playfab/live-service-management/game-configuration/index.md)
- [针对 XBOX 游戏串流优化您的游戏](/zh-CN/build/core-features/common/game-streaming/game-streaming-optimizing-your-game.md)
