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

# XUserGetTokenAndSignatureAsync

> XUserGetTokenAndSignatureAsync

# XUserGetTokenAndSignatureAsync

以异步方式检索授权对目标 URL 的 HTTPS 调用所需的 X 令牌和签名的 API。如果在标题的[合作伙伴中心单一登录页面](/services/xbox-services/fundamentals/s2s-auth-calls/custom-service-config/single-sign-on/live-single-sign-on)中存在与目标 URL 关联的信赖方，则会返回 X 令牌和签名。在发出请求之前，将 [API 的结果版本](/reference/system/xuser/functions/xusergettokenandsignatureresult)中的 X 令牌和签名值添加到 HTTPS 调用的 Authorization 和 Signature 标头中。

## 语法

```cpp theme={null}
HRESULT XUserGetTokenAndSignatureAsync(  
         XUserHandle user,  
         XUserGetTokenAndSignatureOptions options,  
         const char* method,  
         const char* url,  
         size_t headerCount,  
         const XUserGetTokenAndSignatureHttpHeader* headers,  
         size_t bodySize,  
         const void* bodyBuffer,  
         XAsyncBlock* async  
)  
```

### 参数

*user*   \_In\_\
类型：XUserHandle

Web 请求所针对的用户的句柄。

*options*   \_In\_\
类型：[XUserGetTokenAndSignatureOptions](/reference/system/xuser/enums/xusergettokenandsignatureoptions)

用于检索用户令牌和签名的选项。

*method*   \_In\_z\_\
类型：char\*

Web 请求的方法类型。

*url*   \_In\_z\_\
类型：char\*

HTTPS 请求的目标 URL。请为 HTTPS 请求使用完整的 URL 字符串。不要缩写 URL。

*headerCount*   \_In\_\
类型：size\_t

*headers* 参数中的标头数量。

*headers*   \_In\_reads\_opt\_(headerCount)\
类型：[XUserGetTokenAndSignatureHttpHeader\*](/reference/system/xuser/structs/xusergettokenandsignaturehttpheader)

Web 请求的标头。

*bodySize*   \_In\_\
类型：size\_t

*bodyBuffer* 参数中缓冲区的大小。

*bodyBuffer*   \_In\_reads\_bytes\_opt\_(bodySize)\
类型：void\*

包含 Web 请求正文的缓冲区。

*async*   \_Inout\_\
类型：[XAsyncBlock\*](/reference/system/xasync/structs/xasyncblock)

用于轮询调用状态并检索调用结果的 [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)。

### 返回值

类型：HRESULT

如果成功，则返回 S\_OK；否则返回错误代码。
有关错误代码列表，请参阅[错误代码](/reference/errorcodes)。

## 注解

若要检索调用 XUserGetTokenAndSignatureAsync 的结果，请调用 [XUserGetTokenAndSignatureResult](/reference/system/xuser/functions/xusergettokenandsignatureresult)。

若要检索用于存放调用 [XUserGetTokenAndSignatureAsync](/reference/system/xuser/functions/xusergettokenandsignatureasync) 结果所需的缓冲区大小，请调用 [XUserGetTokenAndSignatureResultSize](/reference/system/xuser/functions/xusergettokenandsignatureresultsize)。

以下示例演示如何以异步方式检索用户的令牌和签名。

```cpp theme={null}
HRESULT RequestTokenComplete(XAsyncBlock* abResult)
{
    size_t bufferSize;
    RETURN_IF_FAILED(XUserGetTokenAndSignatureResultSize(abResult, &bufferSize));

    std::vector<uint8_t> buffer(bufferSize);
    XUserGetTokenAndSignatureData* data;
    if (SUCCEEDED_LOG(XUserGetTokenAndSignatureResult(abResult, buffer.size(), buffer.data(), &data, nullptr /*bufferUsed*/)))
    {
        appLog.AddLog("Token: %s\n", data->token);
        if (data->signature != nullptr)
        {    
            appLog.AddLog("Signature: %s\n", data->signature);
        }
    }

    return S_OK;
}

HRESULT RequestTokenAsync(
    XTaskQueueHandle queue,
    const char* url,
    bool forceRefresh)
{
    auto asyncBlock = std::make_unique<XAsyncBlock>();
    ZeroMemory(asyncBlock.get(), sizeof(*asyncBlock));
    asyncBlock->queue = queue;
    asyncBlock->context = this;
    asyncBlock->callback = [](XAsyncBlock* ab)
    {
        LOG_IF_FAILED(static_cast<User*>(ab->context)->RequestTokenComplete(ab));
        delete ab;
    };


    XUserGetTokenAndSignatureOptions options = XUserGetTokenAndSignatureOptions::None;

    if (forceRefresh)
    {
        WI_SET_FLAG(options, XUserGetTokenAndSignatureOptions::ForceRefresh);
    }

    static const XUserGetTokenAndSignatureHttpHeader headers[] =
    {
        { "Accept", "application/json"},
        { "Why", "Because"},
    };

    if (SUCCEEDED_LOG(XUserGetTokenAndSignatureAsync(
        _handle.get(),
        options,
        "GET",
        url,
        ARRAYSIZE(headers),
        headers,
        0,
        nullptr,
        asyncBlock.get())))
    {
        // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* since the callback will take over ownership.
        // If the call fails, the std::unique_ptr will keep ownership and delete the XAsyncBlock*
        asyncBlock.release();
    }

    return S_OK;
}
```

## 要求

**头文件：** XUser.h

**库：** xgameruntime.lib

**支持的平台：** Windows、Steam Deck、XBOX One 系列主机以及 XBOX Series 主机

## 另请参阅

[XUser](/reference/system/xuser/xuser_members)

[XUserGetTokenAndSignatureResult](/reference/system/xuser/functions/xusergettokenandsignatureresult)

[XUserGetTokenAndSignatureResultSize](/reference/system/xuser/functions/xusergettokenandsignatureresultsize)


## Related topics

- [XUserGetTokenAndSignatureOptions](/zh-CN/reference/system/xuser/enums/xusergettokenandsignatureoptions.md)
- [XUserGetTokenAndSignatureData](/zh-CN/reference/system/xuser/structs/xusergettokenandsignaturedata.md)
- [XUserGetTokenAndSignatureResult](/zh-CN/reference/system/xuser/functions/xusergettokenandsignatureresult.md)
- [XUserGetTokenAndSignatureResultSize](/zh-CN/reference/system/xuser/functions/xusergettokenandsignatureresultsize.md)
- [XUserGetTokenAndSignatureHttpHeader](/zh-CN/reference/system/xuser/structs/xusergettokenandsignaturehttpheader.md)
