> ## 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 要求 User Store ID

> 使用 OAuth 2.0 或 XBOX X-token (XSTS) 在您的服務上產生 User Store ID，而不需要透過遊戲用戶端交換權杖來進行驗證。

本文說明您的服務如何直接產生 User Store ID，而不需要透過遊戲用戶端交換存取權杖和金鑰。此方法適用於：

* 流程中沒有遊戲用戶端的體驗
* 將使用者的 XBOX 帳戶連結至您服務中該使用者的帳戶
* 已在用戶端對服務及服務對服務呼叫中使用 XBOX 安全性權杖 (XSTS 或 X-token) 驗證的遊戲和服務

如需 User Store ID 的一般資訊，請參閱[要求 User Store ID 以進行服務對服務驗證](/zh-TW/publishing/xstore-commerce/xstore-requesting-userstoreid)。

## X-token 需求

此程序需要使用委派驗證 X-token 來識別 User Store ID 所代表的使用者。

如需委派驗證 X-token 及其建立方式的詳細資訊，請參閱「從遊戲服務呼叫 XBOX services」文件。

## 產生 User Store ID 的 OAuth 2.0 需求

如果您的服務使用 OAuth 讓使用者以其 Microsoft 帳戶 (MSA) 登入，則需要符合下列條件：

* 使用者必須有與其 Microsoft 帳戶繫結的 XBOX 身分識別，因為 OAuth 認證必須交換為 X-token。
* 您要求的 OAuth 範圍必須同時包含 `xboxlive.signin` 和 `xboxlive.offline_access`，以確保使用者針對此流程給予適當的同意，並能夠擷取所需的 X-token。

如需在您的服務上搭配 OAuth 使用 XBOX services 登入的詳細資訊，請參閱「遊戲網站的 XBOX services 登入」文件。

## 使用 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 登入」文件。

## 使用 X-token 時的步驟

當您擁有可針對您的信賴憑證者識別使用者的 X-token 之後，請使用其 `DelegationToken` 宣告來建立委派驗證 X-token。初始 X-token 可以來自遊戲用戶端或 OAuth 2.0 流程。

建立委派驗證 X-token 的流程概要：

1. 從使用者的 X-token 擷取 DelegationToken 宣告 (`dlt`)。
2. 使用您的商業合作夥伴憑證，從 XSAS 服務取得服務權杖。
3. 將 DelegationToken、服務權杖及目標信賴憑證者 (請參閱表格) 交換為委派驗證 X-token。

如需如何建立委派驗證 X-token 的完整實作詳細資料，請參閱「從遊戲服務呼叫 XBOX services」文件。

在步驟 3 中，您使用的信賴憑證者取決於您要為使用者產生 User Collections ID 還是 User Purchase ID：

| User Store ID 類型 | 委派驗證 X-token 信賴憑證者 | 用來建立 User 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` |

## 要求

取得委派驗證 X-token 之後，請使用您所呼叫服務的對應 URI 來要求 User 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]` 的委派驗證 X-token |
| `Host` | `string` | 必須與所呼叫 URI 的主機相符 |
| `Content-Length` | `number` | 要求本文的長度。 |
| `Content-Type` | `string` | 指定要求和回應類型。目前唯一支援的值為 `application/json`。 |

### 要求本文

| 參數 | 類型 | 描述 | 必要 |
| - | - | - | - |
| `serviceTicket` | `string` | 根據您所呼叫的 URI，您服務的 Collections 存取權杖或 Purchase 存取權杖。如需建立這些存取權杖的資訊，請參閱[了解不同的權杖和對象 URI](/zh-TW/publishing/xstore-commerce/xstore-requesting-userstoreid#understanding-the-different-access-tokens-and-audience-uris)。 | 是 |
| `publisherUserId` | `string` | 寫入 User 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 服務進行服務對服務驗證的 User 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..."}
```

## 另請參閱

[從您的服務管理產品](/zh-TW/publishing/xstore-commerce/xstore-authenticating-service)

[使用 Microsoft Store API 驗證您的服務](/zh-TW/publishing/xstore-commerce/xstore-authenticating-service)

從遊戲服務呼叫 XBOX services

遊戲網站的 XBOX services 登入

[更新 User Store ID 金鑰](/zh-TW/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)


## Related topics

- [使用 X-token 或 OAuth 2.0 请求 User Store ID](/zh-CN/publishing/xstore-commerce/xstore-requesting-userstoreid-oauth.md)
- [X-Token または OAuth 2.0 を使用したユーザー Store ID の要求](/ja-jp/publishing/xstore-commerce/xstore-requesting-userstoreid-oauth.md)
- [X-token 또는 OAuth 2.0으로 User Store ID 요청](/ko/publishing/xstore-commerce/xstore-requesting-userstoreid-oauth.md)
- [Request a User Store ID with X-tokens or OAuth 2.0](/publishing/xstore-commerce/xstore-requesting-userstoreid-oauth.md)
- [Solicitud de un User Store ID con tokens X u OAuth 2.0](/es/publishing/xstore-commerce/xstore-requesting-userstoreid-oauth.md)
