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

 

#6 2021-08-11 16:30:43

ignis
Member
2019-08-19
8

Re: API Login

Hello!
I have the same problem (I try it with API)

Code:

Piwigo 11.5.0 
OS: Linux
PHP: 7.0.33-0ubuntu0.16.04.6 [2021-08-11 16:29:28]
MySQL: 5.7.20-0ubuntu0.16.04.1 [2021-08-11 16:29:28]
Grafikus könyvtár: ImageMagick 6.8.9-9

After the login, the response:

Code:

HTTP/1.1 200 OK
Date: Wed, 11 Aug 2021 14:24:00 GMT
Server: Apache
Set-Cookie: pwg_id=hhktkoeggfn549onpnhta6epv7; path=/piwigo/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: pwg_remember=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/piwigo/
Set-Cookie: pwg_id=2co5oipapeg7gfmlmnvk4hd4t1; path=/piwigo/; HttpOnly
Content-Length: 27
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/plain; charset=utf-8

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

First: I have two pwg_id in the response

Second: I send both to the server but the status response:

Code:

Cookie: pwg_id=hhktkoeggfn549onpnhta6epv7; pwg_id=2co5oipapeg7gfmlmnvk4hd4t1

Code:

"username":"guest"

Thanx
Csaba

Offline

 

#7 2021-08-11 16:36:18

ignis
Member
2019-08-19
8

Re: API Login

Hello!
I did some testing:
If I use only the second Id in the cookies its working.
Why I get two Id in the login response?

Csaba

Offline

 

Board footer

Powered by FluxBB

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