Announcement

  •  » Engine
  •  » [Help] [API] pwg.session.getStatus returns "guest" always

#1 2016-10-15 11:35:25

tip2tail
Member
2012-06-23
5

[Help] [API] pwg.session.getStatus returns "guest" always

Hi,

I am developing a PHP class that queries a Piwigo instance via the API.  This is working perfectly for all but one option - one which requires an admin user to be logged in.

A section of the code from my class is listed below.  The call to "pwg.session.login" returns

Code:

{"stat":"ok","result":true}

However the subsequent call to "pwg.session.getStatus" then returns

Code:

{"stat":"ok","result":{"username":"guest","status":"guest","theme":"hamclassics","language":"en_UK","pwg_token":"40dd2015ff8da963298e18f944fee5cf","charset":"utf-8","current_datetime":"2016-10-15 02:18:33","version":"2.8.2"}}

So I am unable to obtain the token string, and my admin call later in the process fails as such.

What can I do to make this work?

Code:

    public function __construct() {
        $this->Context = stream_context_create();
    }

    public function Login() {
        if ($this->Settings->Username == "") {
            return "";
        }
        $dictQuery = [
            "username" => $this->Settings->Username,
            "password" => $this->Settings->Password
        ];
        $oResponse = $this->PostWebServiceRequest("pwg.session.login", $dictQuery);
        if ($oResponse->result !== true) {
            SetMessage("Login failure", false);
            return false;
        }
        $oResponse = $this->PostWebServiceRequest("pwg.session.getStatus");
        if ($oResponse->result->username != $this->Settings->Username) {
            SetMessage("Login failure - unable to obtain token", false);
            return false;
        }

        // All OK
        $this->Key = $oResponse->result->pwg_token;

        $this->IsLoggedIn = true;
        return true;
    }

    public function PostWebServiceRequest($sMethod, $dictQuery = array(), $bDebug = false) {
        $sPostURL = $this->Settings->PiwigoURL . "ws.php?format=json";
        $dictQuery["method"] = $sMethod;
        $oPostData = http_build_query($dictQuery);
        $oHTTPOpts = array('http' =>
            array(
                'method'  => 'POST',
                'header'  => 'Content-type: application/x-www-form-urlencoded',
                'content' => $oPostData
            )
        );
        stream_context_set_option($this->Context, $oHTTPOpts);
        $sResult = file_get_contents($sPostURL, false, $this->Context);
        $oObject = json_decode($sResult);

        if ($bDebug) {
            // We need to debug this?  OK - Dump everything...
            Dump($sPostURL, $dictQuery, $sResult, $oObject);
        }

        return $oObject;
    }

Piwigo version: Latest
PHP version: 5.5
MySQL version:
Piwigo URL: Internal

Thanks,
Mark

Offline

 

#2 2016-10-15 17:19:09

rvelices
Former Piwigo Team
2005-12-29
1960

Re: [Help] [API] pwg.session.getStatus returns "guest" always

You need to get the cookies sent with the response to your login request and send it in subsequent requests

Offline

 

#3 2016-10-15 17:32:16

tip2tail
Member
2012-06-23
5

Re: [Help] [API] pwg.session.getStatus returns "guest" always

OK, thanks for that - any advice on how to do this in PHP?

Mark

Offline

 

#4 2016-10-15 19:18:06

tip2tail
Member
2012-06-23
5

Re: [Help] [API] pwg.session.getStatus returns "guest" always

Figured it out!!!

Thank you so much!

Mark

Offline

 

#5 2016-11-22 10:10:38

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13791

Re: [Help] [API] pwg.session.getStatus returns "guest" always

tip2tail wrote:

Figured it out!!!

please post your piece of code to do that, I'm 100% sure other people will find it useful, including me ;-)

Offline

 
  •  » Engine
  •  » [Help] [API] pwg.session.getStatus returns "guest" always

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact