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

# X-Token または OAuth 2.0 を使用したユーザー Store ID の要求

> 認証のためにゲーム クライアントを介してトークンを交換せずに、OAuth 2.0 または XBOX X-Token (XSTS) を使用してサービス上でユーザー Store ID を生成します。

この記事では、ゲーム クライアントを通じてアクセス トークンとキーを交換せずに、サービスが直接ユーザー Store ID を生成する方法について説明します。このアプローチは次の場合に役立ちます。

* フロー内にゲーム クライアントが存在しないエクスペリエンス
* ユーザーの XBOX アカウントを、そのユーザーの独自のサービス アカウントにリンクする
* クライアント/サービス間およびサービス間呼び出しに XBOX Security Tokens (XSTS または X-Token) 認証を既に使用しているゲームおよびサービス

ユーザー Store ID の一般的な情報については、[サービス間認証用のユーザー Store ID を要求する](/publishing/xstore-commerce/xstore-requesting-userstoreid)を参照してください。

## X-Token の要件

このプロセスでは、ユーザー Store ID が表すユーザーを識別するために Delegated Auth X-Token の使用が必要です。

Delegated Auth X-Token とその作成方法の詳細については、「Title service calls to XBOX services」のドキュメントを参照してください。

## ユーザー Store ID を生成するための OAuth 2.0 の要件

サービスが OAuth を使用してユーザーが Microsoft アカウント (MSA) でサインインする場合、以下が必要です。

* OAuth 資格情報を X-Token と交換する必要があるため、ユーザーは Microsoft アカウントに紐付けられた XBOX ID を持っている必要があります。
* 要求する OAuth スコープには、このフローに対する適切なユーザー同意と必要な X-Token を取得できるように `xboxlive.signin` と `xboxlive.offline_access` の両方を含める必要があります。

サービスで OAuth を使用した XBOX services サインインの詳細については、「XBOX services sign-in for title websites」のドキュメントを参照してください。

## OAuth 2.0 資格情報を使用する場合の手順

OAuth では、ユーザーの同意を取得し、証明書利用者に対して OAuth 資格情報を X-Token と交換します。その後、[X-Token を使用する場合の手順](#steps-when-using-x-tokens)と同じフローに進みます。

OAuth 資格情報を X-Token と交換する概要フロー:

1. OAuth 2.0 認証コードを取得する
2. 認証コードを RPS トークン (Microsoft アカウント アクセス トークンとも呼ばれる) と交換する
3. RPS トークンを XBOX ユーザー トークンと交換する
4. 独自の証明書利用者に対して XBOX ユーザー トークンを X-Token と交換する
5. [X-Token を使用する場合の手順](#steps-when-using-x-tokens)で X-Token を使用する

完全な実装の詳細については、「XBOX services sign-in for title websites」のドキュメントを参照してください。

## X-Token を使用する場合の手順

証明書利用者向けにユーザーを識別する X-Token を取得したら、その `DelegationToken` クレームを使用して Delegated Auth X-Token を作成します。最初の X-Token は、ゲーム クライアントまたは OAuth 2.0 フローのいずれかから取得できます。

Delegated Auth X-Token を作成する概要フロー:

1. ユーザーの X-Token から DelegationToken クレーム (`dlt`) を抽出します。
2. ビジネス パートナー証明書を使用して XSAS サービスからサービス トークンを取得します。
3. DelegationToken、サービス トークン、および対象の証明書利用者 (表を参照) を Delegated Auth X-Token と交換します。

Delegated Auth X-Token の作成方法の完全な実装の詳細については、「Title service calls to XBOX services」のドキュメントを参照してください。

手順 3 で使用する証明書利用者は、ユーザーの User Collections ID を生成するか User Purchase ID を生成するかによって異なります。

| ユーザー Store ID の種類                         | Delegated Auth X-Token の証明書利用者  | ユーザー Store ID を作成する URI                                           |
| ----------------------------------------- | ------------------------------- | ----------------------------------------------------------------- |
| User Collections ID (`UserCollectionsId`) | `http://licensing.xboxlive.com` | `https://collections.mp.microsoft.com/v7.0/beneficiaries/me/keys` |
| User Purchase ID (`UserPurchaseId`)       | `http://mp.microsoft.com/`      | `https://purchase.mp.microsoft.com/v7.0/users/me/keys`            |

## リクエスト

Delegated Auth X-Token を取得したら、呼び出すサービスに対応する URI を使用してユーザー Store ID を要求します。

### リクエスト構文

| メソッド   | リクエスト URI                                                 |
| ------ | --------------------------------------------------------- |
| `POST` | `collections.mp.microsoft.com/v7.0/beneficiaries/me/keys` |
| `POST` | `purchase.mp.microsoft.com/v7.0/users/me/keys`            |

### リクエスト ヘッダー

| ヘッダー             | 型        | 説明                                                            |
| ---------------- | -------- | ------------------------------------------------------------- |
| `Authorization`  | `string` | 必須。`XBL3.0 x=[User Hash];[Token]` 形式の Delegated Auth X-Token。 |
| `Host`           | `string` | 呼び出す URI のホストと一致する必要があります。                                    |
| `Content-Length` | `number` | リクエスト本文の長さ。                                                   |
| `Content-Type`   | `string` | リクエストとレスポンスの型を指定します。現在サポートされている値は `application/json` のみです。    |

### リクエスト本文

| パラメーター            | 型        | 説明                                                                                                                                                                                                                                                        | 必須  |
| ----------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --- |
| `serviceTicket`   | `string` | 呼び出している URI に基づく、サービスの Collections アクセス トークンまたは Purchase アクセス トークン。これらのアクセス トークンの作成については、[異なるアクセス トークンとオーディエンス URI について](/publishing/xstore-commerce/xstore-requesting-userstoreid#understanding-the-different-access-tokens-and-audience-uris)を参照してください。 | はい  |
| `publisherUserId` | `string` | 独自の追跡のためにユーザー Store ID に記録される任意の値。                                                                                                                                                                                                                        | いいえ |

### リクエストの例

```html theme={null}
POST https://collections.mp.microsoft.com/v7.0/beneficiaries/me/keys HTTP/1.1
Authorization: XBL3.0 x=13178812777611882182;eyJ...
Content-Length: 1352
Content-Type: application/json;
Host: collections.mp.microsoft.com
 
{
    "serviceTicket": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIn...",
    "publisherUserId": "CustomPublisherUserId"
}
```

## レスポンス

### レスポンス本文

| パラメーター | 型        | 説明                                                | 必須 |
| ------ | -------- | ------------------------------------------------- | -- |
| `key`  | `string` | Microsoft Store サービスとのサービス間認証に使用できるユーザー Store ID。 | はい |

### レスポンスの例

```html theme={null}
HTTP/1.1 200 OK
Date: Wed, 23 Mar 2022 23:06:25 GMT
Content-Type: application/json; 
Content-Length: 1716
MS-CorrelationId: dd5be303-f9a0-408a-bd01-e8c63afa721a
MS-RequestId: 0b9c8e5e-dfdd-4cf8-bfbe-93a5be3a7f48
MS-CV: rqFUH3gA+UOmTyXe.0

{"key":"eyJhbGciOiJSUzI1NiIsImtpZCI6IjY..."}
```

## 関連項目

[サービスから製品を管理する](/publishing/xstore-commerce/xstore-authenticating-service)

[Microsoft Store API を使用したサービスの認証](/publishing/xstore-commerce/xstore-authenticating-service)

Title service calls to XBOX services

XBOX services sign-in for title websites

[ユーザー Store ID キーを更新する](/publishing/xstore-commerce/xstore-renew-userstoreid)

[Microsoft.StoreServices ライブラリ](https://github.com/microsoft/Microsoft-Store-Services)

[Microsoft.StoreServices サンプル](https://github.com/microsoft/Microsoft-Store-Services-Sample)
