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

# Request parties from your services with RequestPartyService

> 自身のゲーム サービスから PlayFab RequestPartyService API を呼び出して、マルチプレイヤー チャットとデータ セッションのために Party ネットワークをプロビジョニングするチュートリアル。

このチュートリアルでは、***RequestPartyService API*** を使用してクライアントからだけでなく自身のサービスから Party を要求する手順について説明します。これにより、HTTP 呼び出しを介して PlayFab Party をゲームに統合する方法をより柔軟に制御できます。

* 独自の認証と認可メカニズムを使用したり、Party の作成と参加のユーザー インターフェースとユーザー エクスペリエンスをカスタマイズしたりできます。
* 独自の条件で PlayFab Party を割り当てることができます。サービス側からリレー ネットワークを管理することにより、独自のサービスを使用したり、[PlayStream と CloudScript](/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 機能を有効にします](/services/playfab/multiplayer/networking/enable-party)。
3. Party SDK をダウンロードしてセットアップします。ダウンロード リンクについては、[Party SDK](/services/playfab/multiplayer/networking/party-sdks/overview) を参照してください。

<Note>
  このチュートリアルは、より包括的な [Party クイックスタート](/services/playfab/multiplayer/networking/quickstart) 記事の一部の手順に対する代替プロセスです。
</Note>

### 1. GameManager から PlayFab のタイトル シークレット キーを取得する

1. **[Game Manager](https://developer.playfab.com/)** にサインインします。
2. タイトルを選択します。
3. 右上の歯車アイコンを選択します。
4. **Title settings** を選択し、次に **Secret Keys** タブを選択します。

詳細については、[シークレット キー管理](/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 を Entity Token と交換します。

```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 要求を行った後、クライアントが Party サーバーに接続するために必要なすべての詳細を含む [ネットワーク記述子](https://learn.microsoft.com/en-us/rest/api/playfab/multiplayer/multiplayer-server/request-party-service#requestpartyserviceresponse) を含む応答を受け取ります。

```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 の統合](/services/playfab/multiplayer/networking/party-lobby-integration) のガイダンスに従う必要があります。クイックなテスト目的で Party ネットワーク記述子を [手動で共有](/services/playfab/multiplayer/networking/quickstart#manually-share-party-network-descriptor) することもできますし、ゲームのニーズに基づいて独自のロビー サービスと招待を使用することもできます。

### 6. Party サーバーに接続する

クライアントは、通常どおりネイティブ SDK を使用して、ネットワーク記述子を使用して Party サーバーに接続できます。Party ネットワークへの初めての接続の場合、クイックスタート ガイドの [Party ネットワークに接続する](/services/playfab/multiplayer/networking/quickstart#connect-to-a-party-network) セクションを参照してください。

## 関連項目

* [Party クイックスタート](/services/playfab/multiplayer/networking/quickstart)
* [Party の機能](/services/playfab/multiplayer/networking/party-features)
* [Party サンプル](/services/playfab/multiplayer/networking/party-samples)
* [Multiplayer services](/services/playfab/multiplayer/mpintro)
* [Party の招待とセキュリティ モデル](/services/playfab/multiplayer/networking/concepts-invitations-security-model)
* [Party API リファレンス ドキュメント](/services/playfab/multiplayer/networking/reference/party_members)


## Related topics

- [PartyXblManager::CompleteGetTokenAndSignatureRequest](/ja-jp/services/playfab/multiplayer/networking/xblreference/classes/PartyXblManager/methods/partyxblmanager_completegettokenandsignaturerequest.md)
- [PartyXblTokenAndSignatureRequestedStateChange](/ja-jp/services/playfab/multiplayer/networking/xblreference/structs/partyxbltokenandsignaturerequestedstatechange.md)
- [PlayFab Services SDK リリースノート 2023](/ja-jp/services/playfab/release-notes/2023.md)
- [PartyXblThreadId](/ja-jp/services/playfab/multiplayer/networking/xblreference/enums/partyxblthreadid.md)
- [PartyXblLocalChatUserDestroyedReason](/ja-jp/services/playfab/multiplayer/networking/xblreference/enums/partyxbllocalchatuserdestroyedreason.md)
