Public/New-GW2SubToken.ps1

<#
.SYNOPSIS
    Creates a new subtoken for the Guild Wars 2 API.
 
.DESCRIPTION
    The New-GW2SubToken function requests a subtoken from the Guild Wars 2 API using an existing API key.
    Subtokens allow for restricted access by limiting permissions, setting an expiration date, or restricting usage to specific endpoint URLs.
 
.PARAMETER APIKey
    The primary API key used to authorize the request.
 
.PARAMETER Permissions
    A comma-separated list of permissions to grant to the subtoken (e.g., 'account,chars,inventories').
 
.PARAMETER Expiration
    An ISO-8601 formatted date string specifying when the subtoken should expire.
 
.PARAMETER Urls
    An optional array of URLs that the subtoken is restricted to.
 
.EXAMPLE
    New-GW2SubToken -APIKey "ABC-123" -Permissions "account,characters" -Expiration "2025-01-01T00:00:00Z"
    This example creates a subtoken with account and characters permissions that expires at the start of 2025.
 
.EXAMPLE
    New-GW2SubToken -APIKey "ABC-123" -Permissions "account,characters" -Expiration "2025-01-01T00:00:00Z" -Urls "/v2/account/bank,/v2/account/inventory"
    This example creates a subtoken with account and characters permissions that expires at the start of 2025 and is restricted to the bank and inventory endpoints.
 
#>

function New-GW2SubToken {
    param (
        [Parameter(Mandatory = $true)]
        [string]$APIKey,
        [Parameter(Mandatory = $true)]
        [string]$Permissions,
        [Parameter(Mandatory = $true)]
        [string]$Expiration,
        [Parameter(Mandatory = $false)]
        [string[]]$Urls
    )
    
    $headers = @{
        "Authorization" = "Bearer $APIKey"
    }

    $url = "https://api.guildwars2.com/v2/createsubtoken"

    $url = $url + "?permissions=$Permissions"
    $url = $url + "&expire=$Expiration"
    if ($Urls) {
        $url = $url + "&urls=" + ($Urls -join ",")
    }

    $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers
    return $response
}