TheTvDB API v4 Update (with Corrections)

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 see below for the updated functions. 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.


As pointed out by @Morrison, the original functions I outlined in my previous post require more than just swapping out the URI. Some of the responses are in a different format and need to be handled. With that in mind, I published a new set of functions that do work. My apologies to anyone who was struggling.

8 thoughts on “TheTvDB API v4 Update (with Corrections)”

  1. Thanks for putting this together! However when I copy your code into a PS1 file, and run it, I get nothing spit out on the Powershell ISE cli. So I ran the invoke-webrequest command manually, with the below parameters, but it errors out. Note I don’t have a PIN in the API section of my thetvdb.com website. Does your still work? Do you see any reason why the below command wouldn’t work?

    Invoke-RestMethod -Uri https://api4.thetvdb.com/v4/login -ContentType “application/json” -Headers $Headers -Body ‘”apikey” : “”‘ -Method Post

    Invoke-RestMethod : {“status”:”failure”,”message”:”InvalidJWTToken: invalid credentials”,”data”:{“status”:”failure”,”message”:”InvalidJWTToken: invalid
    credentials”,”data”:””}}

    Reply
  2. PIN seems to be optional in the script, but yes, there is no PIN somewhere in my account. I don’t see mention of a PIN in that link. Does your key/pin combo still work with this script?

    Reply

Leave a Reply

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