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

# 使用 RequestPartyService 從您的服務要求 Party

> 從您自己的遊戲服務呼叫 PlayFab RequestPartyService API，為多人遊戲聊天和資料工作階段佈建 Party 網路的教學課程。

本教學課程說明使用 ***RequestPartyService API*** 從您自己的服務 (而不是嚴格限於從用戶端) 要求 Party 的步驟，讓您能透過 HTTP 呼叫，以更大的彈性和控制權將 PlayFab Party 整合到遊戲中。

* 它可讓您使用自己的驗證和授權機制，或自訂 Party 建立和加入的使用者介面與使用者體驗。
* 它可讓您完全依照自己的條件配置 PlayFab Party。藉由從服務端管理轉送網路，您可以使用自己的服務或在 [PlayStream 和 CloudScript](/zh-TW/services/playfab/data-analytics/acting-data/action-rules-using-cloudscript-actions-with-playstream) 的協助下，有效且可靠地設定複雜的網路拓撲。
* 它讓您能夠將 Party 建立限制為僅限服務，從而針對用戶端的濫用增加額外的防禦。

## 先決條件

您需要 PlayFab 帳戶，並已啟用 Party 功能。

1. 建立或登入您的 [PlayFab 帳戶](https://developer.playfab.com)。
2. 從您的 PlayFab 帳戶[透過 Game Manager 啟用 Party 功能](/zh-TW/services/playfab/multiplayer/networking/enable-party)。
3. 下載並設定 Party SDK。如需下載連結，請參閱 [Party SDK](/zh-TW/services/playfab/multiplayer/networking/party-sdks/overview)。

<Note>
  本教學課程是更完整的 [Party 快速入門](/zh-TW/services/playfab/multiplayer/networking/quickstart)文章中部分步驟的替代程序。
</Note>

### 1. 從 GameManager 取得 PlayFab 遊戲祕密金鑰

1. 登入 **[Game Manager](https://developer.playfab.com/)**。
2. 選取您的遊戲。
3. 在右上角選取齒輪圖示。
4. 選取 **Title settings**，然後選取 **Secret Keys** 索引標籤。

如需詳細資訊，請參閱[祕密金鑰管理](/zh-TW/services/playfab/live-service-management/gamemanager/secret-key-management)。

### 2. 取得 PlayFab 實體權杖

從您的服務使用 [GetEntityToken](https://learn.microsoft.com/en-us/rest/api/playfab/authentication/authentication/get-entity-token) REST API，以遊戲 SecretKey 交換實體權杖。

```js theme={null}
async function getEntityToken(titleId, secretKey) {
  try {

    // Construct the request URL
    const requestUrl =
      `https://${titleId}.playfabapi.com/Authentication/GetEntityToken`;

    // Create headers with your secret key and content type
    const headers = {
      'X-SecretKey': secretKey,
      'Content-Type': 'application/json', // Add this line
    };

    // Make the POST request
    const response = await fetch(requestUrl, {
      method: 'POST',
      headers: headers,
    });

    // Check if the response is successful
    if (response.ok) {
      const responseBody = await response.json();
      console.log(`Entity Token: ${responseBody.data.EntityToken}`);
      return responseBody.data.EntityToken;
    } else {
      console.error(`Error: ${response.status}`);
    }
  } catch (error) {
    console.error(`Exception: ${error.message}`);
  }
}
```

### 3. 發出 POST 要求

使用擷取的實體權杖，以 Party 所需的參數對 [RequestPartyService](https://learn.microsoft.com/en-us/rest/api/playfab/multiplayer/multiplayer-server/request-party-service) 端點發出 POST 要求。

```js theme={null}
async function requestPartyService(titleId, entityToken) {
  try {

    // Construct the request URL
    const requestUrl = `https://${titleId}.playfabapi.com/Party/RequestPartyService`;

    // Create headers with the entity token and content type
    const headers = {
      'X-EntityToken': entityToken,
      'Content-Type': 'application/json',
    };

    // Create the request body with PartyNetworkConfiguration and PreferredRegions
    const requestBody = {
      NetworkConfiguration: {
        MaxUsers: 4,
        MaxDevices: 4,
        MaxDevicesPerUser: 1,
        MaxUsersPerDevice: 1,
      },
      PreferredRegions: ['WestUs'],
    };

    // Make the POST request
    const response = await fetch(requestUrl, {
      method: 'POST',
      headers: headers,
      body: JSON.stringify(requestBody),
    });

    // Check if the response is successful
    if (response.ok) {
      const responseBody = await response.json();
      console.log('Party service response:', responseBody);
    } else {
      console.error(`Error: ${response.status}`);
    }
  } catch (error) {
    console.error(`Exception: ${error.message}`);
  }
  return null;
}
```

### 4. 接收回應

發出 POST 要求之後，您會收到包含[網路描述元](https://learn.microsoft.com/en-us/rest/api/playfab/multiplayer/multiplayer-server/request-party-service#requestpartyserviceresponse)的回應，其中包含用戶端連線到 Party 伺服器所需的所有詳細資料。

```json theme={null}
{
    "code": 200,
    "status": "OK",
    "data": {
        "PartyId": "0543645b-9004-4784-95d7-459cef4a23fa",
        "SerializedNetworkDescriptor": "AwBnP28mW2RDBQSQhEeV10Wc70oj+ldlc3RVcwAAAAAAAAAAAAAAAAAAPXqiEpapxSC+Owo1h8qJ5NuEOmtS0oeJqq8h6q5ybZZrbktkbnMtaXIwLTgxZTUtNTI3MWZiZDYtMmMyYS00NGE0LThhNjAtNmZlZjI4ZDAzMjc5Lndlc3R1cy5jbG91ZGFwcC5henVyZS5jb20=",
        "InvitationId": "e95366d4-c9ed-4a6e-94f6-215c1d69d467"
    }
}
```

### 5. 分享網路描述元

與網路的任何潛在參與者分享網路描述元。有多種方式可以做到這一點。使用 PlayFab Lobby 的用戶端應遵循[將 Party 與 Lobby 整合](/zh-TW/services/playfab/multiplayer/networking/party-lobby-integration)中的指引。您也可以[手動分享](/zh-TW/services/playfab/multiplayer/networking/quickstart#manually-share-party-network-descriptor) Party 網路描述元以進行快速測試，或根據遊戲需求使用您自己的大廳服務和邀請。

### 6. 連線到 Party 伺服器

用戶端可以照常使用原生 SDK，透過網路描述元連線到 Party 伺服器。如果這是您第一次連線到 Party 網路，請參閱快速入門指南的[連線到 Party 網路](/zh-TW/services/playfab/multiplayer/networking/quickstart#connect-to-a-party-network)一節。

## 另請參閱

* [Party 快速入門](/zh-TW/services/playfab/multiplayer/networking/quickstart)
* [Party 功能](/zh-TW/services/playfab/multiplayer/networking/party-features)
* [Party 範例](/zh-TW/services/playfab/multiplayer/networking/party-samples)
* [多人遊戲服務](/zh-TW/services/playfab/multiplayer/mpintro)
* [Party 邀請與安全性模型](/zh-TW/services/playfab/multiplayer/networking/concepts-invitations-security-model)
* [Party API 參考文件](/zh-TW/services/playfab/multiplayer/networking/reference/party_members)


## Related topics

- [要求 User Store ID 以進行服務對服務驗證](/zh-TW/publishing/xstore-commerce/xstore-requesting-userstoreid.md)
- [更新 User Store ID 金鑰](/zh-TW/publishing/xstore-commerce/xstore-renew-userstoreid.md)
- [從您的服務管理消耗性產品](/zh-TW/publishing/xstore-commerce/xstore-managing-consumables.md)
- [從您的服務偵測 XBOX Game Pass 訂閱存取權](/zh-TW/publishing/xstore-commerce/xstore-detecting-game-pass.md)
- [從您的服務查詢使用者權利](/zh-TW/publishing/xstore-commerce/xstore-query-entitlements.md)
