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 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.https://api.thetvdb.com
to https://api4.thetvdb.com/v4
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.
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”:””}}
You’ll need to get an API key and PIN. After you log in with them there’s a way to request a key. Since it’s for personal user it should be free.
Without providing my key, that is what I did (https://imgur.com/a/DP5kmQt ). Does the current code still work with your key?
It’s not obvious where you PIN is. I’ve documented it on the accompanying GitHub repository: https://github.com/kmsigma/PoshTvDb
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?
PIN wasn’t supposed to be optional. That’s on me. Most of the stuff I’m doing is now in the GitHub repository. I can test this script tomorrow.
==UPDATE==
Hi @Morrison. I just checked and, yes. My edits were FAR outside just swapping the header information. I’ve uploaded a set of three functions (Get-TvDbAuthenticationHeaderv4.ps1, Get-TvDbEpisodeData.ps1, and Get-TvDbSeriesName.ps1) to my GitHub repository.
I’ve also published a little “testing” script you can download/copy to test your API/PIN combo out. To use them, create a folder on your computer somewhere for the script. I’ve outlined how you need to organize the files on your computer to test in the README document.
Thanks. Sounds like PIN is still needed though, and I still don’t see this anywhere in my account. Could I trouble you to post a redacted screenshot of where it is in your’s?
The pictures are up in the GitHub repository details.
https://github.com/kmsigma/PoshTvDb