public/helper/Send-TwitterOauth2_Token.ps1

function Send-TwitterOauth2_Token {
<#
.SYNOPSIS
    Authentication

.DESCRIPTION
    POST oauth2/token
    
    Allows a registered application to obtain an OAuth 2 Bearer Token, which can be used to make API requests on an application's own behalf, without a user context. This is called Application-only authentication.
    
    A Bearer Token may be invalidated using oauth2/invalidate_token. Once a Bearer Token has been invalidated, new creation attempts will yield a different Bearer Token and usage of the previous token will no longer be allowed.
    
    Only one bearer token may exist outstanding for an application, and repeated requests to this method will yield the same already-existent token until it has been invalidated.
    
    Successful responses include a JSON-structure describing the awarded Bearer Token.
    
    Tokens received by this method should be cached. If attempted too frequently, requests will be rejected with a HTTP 403 with code 99.

.PARAMETER grant_type
    Specifies the type of grant being requested by the application. At this time, only client_credentials is allowed. See Application-Only Authentication for more information.

.NOTES
    This helper function was generated by the information provided here:
    https://developer.twitter.com/en/docs/basics/authentication/api-reference/token

#>

    [CmdletBinding()]
    Param(
        [string]$grant_type
    )
    Begin {

        [hashtable]$Parameters = $PSBoundParameters
                   $CmdletBindingParameters | ForEach-Object { $Parameters.Remove($_) }

        [string]$Method      = 'POST'
        [string]$Resource    = '/oauth2/token'
        [string]$ResourceUrl = 'https://api.twitter.com/oauth2/token'

    }
    Process {

        # Find & Replace any ResourceUrl parameters.
        $UrlParameters = [regex]::Matches($ResourceUrl, '(?<!\w):\w+')
        ForEach ($UrlParameter in $UrlParameters) {
            $UrlParameterValue = $Parameters["$($UrlParameter.Value.TrimStart(":"))"]
            $ResourceUrl = $ResourceUrl -Replace $UrlParameter.Value, $UrlParameterValue
        }

        $OAuthSettings = Get-TwitterOAuthSettings -Resource $Resource
        Invoke-TwitterAPI -Method $Method -ResourceUrl $ResourceUrl -Parameters $Parameters -OAuthSettings $OAuthSettings

    }
    End {

    }
}