Hi,
I am trying to get pwg.images.addSimple working from a Ruby script but running into problems with session and cookies, from what I been able to find in the Ruby doc this is the script I put together.
require 'net/http' require 'pp' http = Net::HTTP.new('alastairhm.no-ip.org',80) path = '/piwigo/ws.php' data = 'method=pwg.session.login&username=admin&password=xxxxxx' headers = { 'Content-Type' => 'application/x-www-form-urlencoded' } response, data = http.post(path, data, headers) pp response if (response.code == '200') cookie = response.response['set-cookie'] pp cookie data = 'method=pwg.images.addSimple&image=dognose.jpg&category=7' headers = { 'Cookie' => cookie, 'Content-Type' => 'application/x-www-form-urlencoded', } response, data = http.post(path, data, headers) pp response end
Put when I run it this is the output I get;
$ ruby piwigo.rb #<Net::HTTPOK 200 OK readbody=true> "pwg_id=nbh50k2j22d4217qnju418luh3; path=/piwigo/; HttpOnly, pwg_remember=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/piwigo/, pwg_id=mgj3ctr36380k1ue66sh6m52r2; path=/piwigo/; HttpOnly" #<Net::HTTPUnauthorized 401 Unauthorized readbody=true>
Any ideas where I am going wrong?
Offline
Hi alastair_hm,
I don't know Ruby, but maybe you can get inspiration from the Perl code I wrote on http://piwigo.org/doc/doku.php?id=dev:w … .addsimple
I can also find you example in C++ (digiKam), Java (reGalAndroid) or Vala (shotwell).
Offline
Thanks, I saw our Perl example that's what I was using to try to put the Ruby version together.
With it getting a 401 back I am assuming it's not using the cookie/ session setup with the first post request.
Offline
alastair_hm wrote:
Thanks, I saw our Perl example that's what I was using to try to put the Ruby version together.
With it getting a 401 back I am assuming it's not using the cookie/ session setup with the first post request.
Your code seems correct but you need to simulate an upload with a input of type file with name image. You forgot it.
Offline
In addition to nicolas reply, I would like to know if you have tried another simpler method, such as pwg.categories.getAdminList? (it requires to be connected as admin, of course)
Offline
plg wrote:
In addition to nicolas reply, I would like to know if you have tried another simpler method, such as pwg.categories.getAdminList? (it requires to be connected as admin, of course)
It works. I tried and get the list of images.
But of course I let alastair_hm try himslef !
Last edited by nicolas (2013-05-19 22:34:08)
Offline
Tried this code but still getting a 401 response;
require 'net/http' require 'pp' http = Net::HTTP.new('alastairhm.no-ip.org',80) path = '/piwigo/ws.php' data = 'method=pwg.session.login&username=admin&password=xxxxx' resp, data = http.post(path, data, {}) puts resp.code puts resp.message if (resp.code == '200') cookie = resp.response['set-cookie'] puts cookie data = 'method=pwg.categories.getAdminList' headers = { "Cookie" => cookie } resp, data = http.post(path, data, headers) puts resp.code puts resp.message end d
$ ruby p2.rb 200 OK pwg_id=pqooke8psqtgsupr87to3h0dr5; path=/piwigo/; HttpOnly, pwg_remember=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/piwigo/, pwg_id=m6fkc71ggov04titpm4l0gt9p5; path=/piwigo/; HttpOnly 401 Unauthorized
Could you possibly post the code you used to get your results?
Offline
I didn't a simpler way to upload an image. I tried net/http/post/multipart but didn't completly succeed. Anyway the code I used to test pwg.categories.getImages :
#!/usr/bin/ruby require 'json' require 'net/http' require 'pp' path = '/dev/piwigo/ws.php?format=json' username = 'xxx' password = 'yyy' http = Net::HTTP.new('localhost', 80) data = "method=pwg.session.login&username=#{username}&password=#{password}" headers = { 'Content-Type' => 'application/x-www-form-urlencoded' } response, data = http.post(path, data, headers) result = JSON.parse(response.body) if response.code.to_i == 200 && (result.has_key? 'stat') # get images from album cookie = response.response['set-cookie'] headers = { 'Cookie' => cookie, 'Content-Type' => 'application/x-www-form-urlencoded', } data = "format=json&method=pwg.categories.getImages&category=11" response, data = http.post(path, data, headers) result = JSON.parse(response.body) pp result end
I never write more than 5 lines of ruby before, so take that as an example only.
Offline
pwg.categories.getImages doesn't require to be authentified, this is why I suggested to use pwg.categories.getAdminList.
The first problem is 401 permission denied, not the upload itself.
Offline
plg wrote:
pwg.categories.getImages doesn't require to be authentified, this is why I suggested to use pwg.categories.getAdminList.
The first problem is 401 permission denied, not the upload itself.
I didn't notice that. Anyway I'm a newbie in ruby so with pwg.categories.getAdminList, it doesn't work. :-(
Offline
Server response contains two pwgid.
You should set cookie 2nd pwgid only.
Sample code is here.
require 'json'
require 'net/http'
path = '/public_html/gallery/ws.php?format=json'
username = 'admin'
password = 'password'
host = 'localhost'
port = 80
http = Net::HTTP.new(host, port)
data = "method=pwg.session.login&username=#{username}&password=#{password}"
headers = {
'Content-Type' => 'application/x-www-form-urlencoded'
}
response, data = http.post(path, data, headers)
result = JSON.parse(response.body)
if response.code.to_i == 200 && (result.has_key? 'stat')
field = response.response['set-cookie'].split(",")
id = field[3].split(";")[0]
headers = {
'Cookie' => id,
'Content-Type' => 'application/x-www-form-urlencoded',
}
data = "&method=pwg.categories.getAdminList"
response, data = http.post(path, data, headers)
pp result = JSON.parse(response.body)
end