plex-api icon indicating copy to clipboard operation
plex-api copied to clipboard

Returns boolean(false) when try to auth to plex.tv

Open vladshoob opened this issue 6 years ago • 5 comments

Good time of yours,

thanks for your work, and if I could on my own, I would gladly solve this and contribute. But I can't figure out what is wrong.

For some reason, I can not login to plex.tv, so that login call return empty response.

Maybe POST params for login has been changed.

vladshoob avatar May 16 '18 13:05 vladshoob

I just confirmed that I CAN auth with the current version of the code.

Are you able to show a code snippet of how you are trying to use the lib?

lhilton avatar May 16 '18 16:05 lhilton

$client = new PlexApi(LOCAL_SERVER_IP, LOCAL_SERVER_PORT, IS_LOCAL_SERVER_HTTPS); $client->setAuth(LOGIN, PASSWORD); $output = $client->getBaseInfo(); dd($output);

It is as in basic examples. Seems like I am receiving an empty response with login post curl.

I did face the very similar issue, but not with my PC or provider some days ago. I have tried to solve why file_get_contents($path) (or curl as well) receives empty response body ("") from graph facebook after theirs 'terms of use' update.

vladshoob avatar May 19 '18 16:05 vladshoob

Hm, it is ok now =(

Probably I just had my plex media server closed or something like that.

Any ideas how to call plex.tv for info, instead of local server?

Thanks in advance!

vladshoob avatar May 19 '18 17:05 vladshoob

I experience the same issue, more often than not.

Here is the code:

#!/usr/bin/env php
<?php

define('FAVORITES_TITLE', 'Share Favorites') ;
define('GENERAL_TITLE', 'Movies') ;
define('PLEX_DATABASE_SERVER', 'web') ;
define('PLEX_HOST', 'r2d2.lovelady.com') ;

//######################################################################################
// https://github.com/jc21/plex-api/blob/master/docs/Documentation.md
//######################################################################################


require_once('lovefunctions.php') ;
require_once('vendor/autoload.php') ;

use jc21\PlexApi ;
use jc21\Util\Filter ;

$pdo = $lov->pdoConnect(PLEX_DATABASE_SERVER) ;
// Get the latest login credentials for this host
$sql = <<<EOS
            SELECT cred.hostname, effective, userId, password
            FROM   plexCredentials cred
            WHERE  cred.hostname = ?
               AND effective <= CURRENT_TIMESTAMP
            ORDER BY effective DESC
            LIMIT 1
            EOS ;
$getCredsStmt = $lov->pdoPrepare($pdo, $sql) ;
$getCredsStmt->execute( [ PLEX_HOST ] ) ;
$nbrRows = $getCredsStmt->rowCount() ;
if ($nbrRows == 0) {
    printf("Could not obtain PLEX credentials from database.\nSQL: %s", $getCredsStmt->queryString) ;
    exit(1) ;
    }
elseif ($nbrRows > 1) {
    printf("%d rows returned requesting PLEX credentials from database.\nSQL: %s", $nbrRows, $getCredsStmt->queryString) ;
    exit(1) ;
    }

$creds = $getCredsStmt->fetch(PDO::FETCH_ASSOC) ;
$client = new PlexApi($creds['hostname']);
$client->setAuth($creds['userId'], $creds['password'] ) ;
if (isset($creds['token']))
    $client->setToken($creds['token']) ;

for ($i=0; $i<10; $i++) { // Because sometimes the login just doesn't work, but after several retries all is well.
    $librarySections = $client->getLibrarySections();
    if (is_array($librarySections) && isset($librarySections['Directory']) && count($librarySections['Directory'])>0) {
        break ;
        }
    if ( ! ($token = $client->getToken()))
        printf("Login was not successful.\n") ;
    elseif ($debug)
        printf("No library sections found. (%d)\n", $i) ;
    }
if ( ! ($token = $client->getToken())) {
    printf("Unable to log in.\n") ;
    exit(1) ;
    }
printf("Logged in.\n") ;

And here is sample output from a few executions:

> testFavorite
Logged in.
dennis@r2d2:/www/ 10/20 13:28:27
> testFavorite
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Unable to log in.
dennis@r2d2:/www/ 10/20 13:28:45
> testFavorite
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Unable to log in.
dennis@r2d2:/www/ 10/20 13:28:58
> testFavorite
Login was not successful.
Login was not successful.  
Logged in.
dennis@r2d2:/www/ 10/20 13:29:09
> testFavorite
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Login was not successful.
Unable to log in.
dennis@r2d2:/www/ 10/20 13:29:20
> testFavorite
Logged in.

I don't know how to troubleshoot this, to figure out what's gone wrong. How can I diagnose this?

(To get around it, I have saved the token in the database and changed the SELECT accordingly...)

            SELECT cred.hostname, effective, userId, password, token

But it's a sad day when Plex invalidates the token, which it sometimes does.

delovelady avatar Oct 20 '22 17:10 delovelady

In addition, I found that when the login fails, this is not detected by PlexApi::call() so the latest curl result covers up the result from the failed login attempt. This code seems to resolve that issue (but of course not the login issue).

public function call($path, $params = [], $method = self::GET, $isLoginCall = false)
{
    if (!$this->token && !$isLoginCall) {
        $this->call('https://plex.tv/users/sign_in.xml', [], self::POST, true);
        if ( ! $this->token) // If login failed, then don't try to execute whatever's next
            return false ;   // Instead, just return false (we will anyway, in the end.
    }

delovelady avatar Oct 20 '22 19:10 delovelady