Announcement

#1 2018-11-29 15:12:12

Nati
Member
2018-11-29
4

API Login

Hello,

I try to add a new user to Piwigo from an external website. But i don't understand how to use the API for login an admin user.

The service pwg.session.login return correctly {"stat":"ok","result":true} but the service pwg.session.getStatus return me as a guest...

I read something about Cookie but i didn't find any good code exemple on how it's work.

Do you have an exemple of connexion with API in PHP ?

Offline

 

#2 2018-12-05 11:09:27

Nati
Member
2018-11-29
4

Re: API Login

I finaly get it ! I hope it can help someone...

// LOGIN
$WSurl = 'https://casmorges.piwigo.com/ws.php?format=json';
$WSdata = "method=pwg.session.login&username=" . $user . "&password=" . $password;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $WSurl);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_REFERER, $WSurl);
curl_setopt($ch, CURLOPT_POSTFIELDS, $WSdata);
curl_setopt($ch, CURLOPT_POST, 1);
$resultLogin = curl_exec($ch);

//GET COOKIE
preg_match_all('|Set-Cookie: (.*);|U', $resultLogin, $results);
$cookie = $results[1][1];
           
curl_close($ch);

// GET STATUS
$WSurl = 'https://casmorges.piwigo.com/ws.php?format=json';
$WSdata = "method=pwg.session.getStatus";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $WSurl);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $cookie); // Here passe the cookie
curl_setopt($ch, CURLOPT_REFERER, $WSurl);
curl_setopt($ch, CURLOPT_POSTFIELDS, $WSdata);
$resultLogin = curl_exec($ch);

curl_close($ch);

Offline

 

#3 2018-12-06 03:19:36

justice41
Member
2018-11-19
23

Re: API Login

Unfortunately doesn't seem to work on me... I am getting a  'stat:ok'result:{---user_details---here}' but users are still not logged in.....

Offline

 

#4 2018-12-06 09:29:34

Nati
Member
2018-11-29
4

Re: API Login

If the service pwg.session.getStatus return the user detail (not the guest) it's work. After the login you need to keep the cookie and use it in every request (pwg.user.add for exemple).

But you are not logged in the Piwigo Website.

Offline

 

#5 2018-12-06 13:32:31

justice41
Member
2018-11-19
23

Re: API Login

Owhh... so the api doesn't really login the user to the website itself then.... looks a custom bridge plugin is what i need.... o.O

Offline

 

Board footer

Powered by FluxBB

github twitter facebook google+ newsletter Donate Piwigo.org © 2002-2018 · Contact