functions/Set-BBRESTSession.ps1

<#
.Synopsis
   Sets the session for the current enviroinment from the Blackboard RESTAPI. This will be used for any Invoke-BBRESTMethod Calls that are made.
   It uses the values stored in Get-BBResconfig. These values can be updated using Set-BBRestConfig.
.DESCRIPTION
   Sets the session for the current enviroinment from the Blackboard RESTAPI. This will be used for any Invoke-BBRESTMethod Calls that are made.
   It uses the values stored in Get-BBResconfig. These values can be updated using Set-BBRestConfig.
.EXAMPLE
   Set-BBRESTSession -verbose
#>

function Set-BBRESTSession
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
    )

    Begin
    {
    }
    Process
    {
        $securePwd = ConvertTo-SecureString "$($BBRESTVariables.ClientSecret)" -AsPlainText -Force
        $credential = New-Object System.Management.Automation.PSCredential ($($BBRESTVariables.ClientID), $securePwd)

        $Uri = "$($BBRESTVariables.ClientURL)/learn/api/public/v1/oauth2/token?&redirect_uri=" + [System.Web.HttpUtility]::UrlEncode("https://localhost")

        $Authorization =
            Invoke-RestMethod   -Method Post `
                                -ContentType application/x-www-form-urlencoded `
                                -Uri $Uri `
                                -Body "grant_type=client_credentials" `
                                -Credential $credential `
                                -Authentication Basic

        $BBRESTVariables.BearerToken = $Authorization.access_token;
        $BBRESTVariables.BearerTokenRefreshTime = (Get-Date).AddSeconds($Authorization.expires_in)

        Write-Verbose "Created Token $($BBRESTVariables.BearerToken)"
    }
    End
    {
    }
}