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

API that asynchronously retrieves the X-token and Signature required to authorize an HTTPS call to the target URL. Returns an X-token and Signature if there's a Relying Party tied to the target URL in the title’s [Single Sign-on page in Partner Center](/services/xbox-services/fundamentals/s2s-auth-calls/custom-service-config/single-sign-on/live-single-sign-on). Add the X-token and Signature values from the [Results version of the API](/reference/system/xuser/functions/xusergettokenandsignatureresult) to the Authorization and Signature headers of the HTTPS call before making the request.

## Syntax

```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  
)  
```

### Parameters

*user*   \_In\_\
Type: XUserHandle

A handle to the user that the web request is for.

*options*   \_In\_\
Type: [XUserGetTokenAndSignatureOptions](/reference/system/xuser/enums/xusergettokenandsignatureoptions)

Options for retrieving the user token and signature.

*method*   \_In\_z\_\
Type: char\*

The method type for the web request.

*url*   \_In\_z\_\
Type: char\*

The target URL of your HTTPS request. Use the full URL string for the HTTPS request. Don't abbreviate the URL.

*headerCount*   \_In\_\
Type: size\_t

The number of headers in the *headers* parameter.

*headers*   \_In\_reads\_opt\_(headerCount)\
Type: [XUserGetTokenAndSignatureHttpHeader\*](/reference/system/xuser/structs/xusergettokenandsignaturehttpheader)

Headers for the web request.

*bodySize*   \_In\_\
Type: size\_t

The size of the buffer in the *bodyBuffer* parameter.

*bodyBuffer*   \_In\_reads\_bytes\_opt\_(bodySize)\
Type: void\*

A buffer that contains the body of the web request.

*async*   \_Inout\_\
Type: [XAsyncBlock\*](/reference/system/xasync/structs/xasyncblock)

An [XAsyncBlock](/reference/system/xasync/structs/xasyncblock) for polling the call's status and retrieving call results.

### Return value

Type: HRESULT

Returns S\_OK if successful; otherwise, returns an error code.
For a list of error codes, see [Error Codes](/reference/errorcodes).

## Remarks

To retrieve the result of calling XUserGetTokenAndSignatureAsync, call [XUserGetTokenAndSignatureResult](/reference/system/xuser/functions/xusergettokenandsignatureresult).

To retrieve the required buffer size to hold the results of calling [XUserGetTokenAndSignatureAsync](/reference/system/xuser/functions/xusergettokenandsignatureasync), call [XUserGetTokenAndSignatureResultSize](/reference/system/xuser/functions/xusergettokenandsignatureresultsize).

The following example demonstrates how to asynchronously retrieve the token and Signature of a user.

```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;
}
```

## Requirements

**Header:** XUser.h

**Library:** xgameruntime.lib

**Supported platforms:** Windows, Steam Deck, XBOX One family consoles, and XBOX Series consoles

## See also

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

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

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


## Related topics

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