TheTvDB API v4 Update

A while ago, theTvDb changed their API call to a new version (v4) and I updated my code. However, I forgot to share it with everyone.

You’ll need your v4 API Key and PIN, to create the authorization. The only changes to other functions would be the URI will now need to point to the new API endpoint.

<#
.Synopsis
   Build an authentication header for use with theTvDb's API for v4 of their API
.DESCRIPTION
   More information about working with the API and how to find these keys can be found here: https://www.thetvdb.com/api-information
.EXAMPLE
   Get-TvDbAuthenticationHeader -ApiKey 'YourAPIKey' -Pin 'YourPin'

Name                           Value                                                                                                                                                          
----                           -----                                                                                                                                                          
Accept                         application/json                                                                                                                                               
Authorization                  Bearer ABunchOfStuffHereThatsImportant

.EXAMPLE
   Get-TvDbSeriesName -Name "Limitless" -ExactMatch -Authorization ( Get-TvDbAuthenticationHeaderv4 -ApiKey 'YourAPIKey' -Pin 'YourPin' )

    id seriesName
    -- ----------
295743 Limitless 
#>
function Get-TvDbAuthenticationHeaderv4 {
    [CmdletBinding()]
    Param
    (
        # Api Key Help
        [Parameter(Mandatory = $true,
            ValueFromPipelineByPropertyName = $false,
            Position = 0)]
        [string]$ApiKey,

        # PIN Help
        [Parameter(Mandatory = $false,
            ValueFromPipelineByPropertyName = $false,
            Position = 1)]
        [string]$Pin
    )

    Begin {
    
        if ( -not ( Test-Connection -ComputerName api4.thetvdb.com -Quiet -Count 1 ) ) {
            Write-Error -Message "Cannot connect to theTvDb site.  Check your internet connection."
            break
        }

    }
    Process {
        $ApiUrl = "https://api4.thetvdb.com/v4"
        $Action = "/login"
        $Headers = @{
            "Accept" = "application/json"
        }
        $Body = @"
{
  "apikey": "$ApiKey",
  "pin": "$Pin"
}
"@

        $Login = Invoke-RestMethod -Uri ( $ApiUrl + $Action ) -ContentType "application/json" -Headers $Headers -Body $Body -Method Post
        if ( $Login ) {
            #retrieve the token
            $LoginToken = $Login.data.token

            # build the headers
            $Headers = @{
                "Accept"        = "application/json"
                "Authorization" = "Bearer $LoginToken"
            }
            # Return the headers
            $Headers
        }
    }
    End {
    }
}

In each of the other functions I outlined in PowerShell & TheTvDB API, you’ll need to change the root URI from https://api.thetvdb.com to https://api4.thetvdb.com/v4. I’m thinking of moving this all into a GitHub. If I do, I’ll put a link here. I’ll also put a link in the other post.

I’ve decided enough was enough and put my code in a GitHub Repository.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.