Using the TVDB API with Delphi

A place for developers to advertise their TheTVDB.com enabled app and get help from other developers with the API.
Prometheus
Posts: 1
Joined: Mon Mar 04, 2019 2:43 am

Using the TVDB API with Delphi

Postby Prometheus » Mon Mar 04, 2019 3:15 am

I have my API key, and my user key. When I test it in the "Authentication : Obtaining and refreshing your JWT token" section of the api documentation (https://api.thetvdb.com/swagger), then I do get a token in the response body.


However, when I attempt to post exactly the same the same JSON data authentication string:

Code: Select all

{
   "apikey": "XXXXXXXXXXXXXXXX",
   "userkey": "YYYYYYYYYYYYYYYYYY"
   "userkey": ""
}

containing my user key and api key from a prototype delphi application (using the "TNetHTTPClient" component), it perpetually returns "{"Error":"Not authorized"}"

Code: Select all

procedure TestResponse;
var
  APrimaryJsonObj : TJsonObject;
  LHTTP: TNetHTTPClient;
  JSONData: TStringList;
begin
  JSONData := TStringList.Create;
  APrimaryJsonObj := TJsonObject.Create();
  LHTTP := TNetHTTPClient.Create(nil);
  try
    APrimaryJsonObj.AddPair(TJSONPair.Create('apikey', 'XXXXXXXXXXXXXXXX'));
    APrimaryJsonObj.AddPair(TJSONPair.Create('username', 'YYYYYYYYYYYYYYYYYY'));
    APrimaryJsonObj.AddPair(TJSONPair.Create('userkey', ''));


    JSONData.Text := APrimaryJsonObj.ToString;

    LHTTP.ContentType := 'application/json';
    LHTTP.Accept := 'application/json';

    Memo1.Lines.Text := JSONData.Text;
    Memo2.Lines.Text := LHTTP.Post('https://api.thetvdb.com/login', JSONData).ContentAsString;
  finally
    LHTTP.Free;
    APrimaryJsonObj.Free;
    JSONData.Free;
  end;
end;


If I want to retrieve the information of Season 1 for Stargate SG-1 on TheMovieDB, I simply have to "download" https://api.themoviedb.org/3/tv/4629/se ... APIKEYXXXX for processing using the following code:

I do not need to provide any kind of login or authorization. Just this:

Code: Select all

AUrl := MovieDB_SeasonDetails_URL(FMovieDb_SeriesID, ASearchSeason);
ProgressPrimary('Downloading Season Metadata');
if DownloadFile(AUrl, AMemoryStream, Self) then
  begin
     {}
  end


Is there NO way at all for me to use the new TVDB JSON API in a similar manner? Or perhaps someones here can show me how to properly use the Delphi "TNetHTTPClient" component for processing all the available options of the JSON API?

JeffOvercash
Posts: 1
Joined: Sat Jul 13, 2019 2:45 pm

Re: Using the TVDB API with Delphi

Postby JeffOvercash » Sat Jul 13, 2019 3:03 pm

It looks like you are not encoding the TStringList to be UTF8. This seems to be important. I used a TStringStream instead cause it is easier to set the encoding. This worked for me. I used the userkey (Unique id from https://www.thetvdb.com/member/api) instead of username (which requires a password too)

Code: Select all

procedure TForm9.btn2Click(Sender: TObject);
var
  APrimaryJsonObj : TJsonObject;
  LHTTP: TNetHTTPClient;
  JSONData: TStringStream;
begin
  APrimaryJsonObj := TJsonObject.Create();
  LHTTP := TNetHTTPClient.Create(nil);
  try
    APrimaryJsonObj.AddPair(TJSONPair.Create('apikey', 'XXXXXXXXXXX'));
    APrimaryJsonObj.AddPair(TJSONPair.Create('userkey', 'YYYYYYYYYYYYYYY'));
    APrimaryJsonObj.AddPair(TJSONPair.Create('username', ''));

    JSONData := TStringStream.Create(APrimaryJsonObj.ToString, TEncoding.UTF8);

    LHTTP.ContentType := 'application/json';
    LHTTP.Accept := 'application/json';

    mmo1.Lines.Text := LHTTP.Post('https://api.thetvdb.com/login', JSONData).ContentAsString;
  finally
    LHTTP.Free;
    APrimaryJsonObj.Free;
    JSONData.Free;
  end;
end;