Announcement

#1 2013-05-16 14:51:37

alastair_hm
Member
2013-04-29
5

Using the web api from Ruby

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.

Code:

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;

Code:

$ 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

 

#2 2013-05-16 14:56:01

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

Re: Using the web api from Ruby

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).


Latest blog post (November 9th 2018) Why Flickr could not remain free for ever

Offline

 

#3 2013-05-16 15:01:24

alastair_hm
Member
2013-04-29
5

Re: Using the web api from Ruby

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

 

#4 2013-05-19 22:12:19

nicolas
Former Piwigo Team
2004-12-30
1176

Re: Using the web api from Ruby

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

 

#5 2013-05-19 22:22:04

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

Re: Using the web api from Ruby

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)


Latest blog post (November 9th 2018) Why Flickr could not remain free for ever

Offline

 

#6 2013-05-19 22:32:56

nicolas
Former Piwigo Team
2004-12-30
1176

Re: Using the web api from Ruby

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

 

#7 2013-05-20 15:32:05

alastair_hm
Member
2013-04-29
5

Re: Using the web api from Ruby

Tried this code but still getting a 401 response;

Code:

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

Code:

$ 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

 

#8 2013-05-20 20:44:47

nicolas
Former Piwigo Team
2004-12-30
1176

Re: Using the web api from Ruby

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 :

Code:

#!/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

 

#9 2013-05-20 20:52:05

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

Re: Using the web api from Ruby

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.


Latest blog post (November 9th 2018) Why Flickr could not remain free for ever

Offline

 

#10 2013-05-20 21:18:37

nicolas
Former Piwigo Team
2004-12-30
1176

Re: Using the web api from Ruby

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

 

#11 2014-11-03 12:18:39

ma2tak
Guest

Re: Using the web api from Ruby

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

 

Board footer

Powered by FluxBB

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