REST API 403 from java SE using Apache http library

DickMcMullen
DickMcMullen's picture

Joined: 2010-08-17
Posts: 21
Posted: Wed, 2010-08-18 01:44

I'm hoping I've made a rookie mistake here. I've got a new RC2 installation running, and it is looking good.

I am working on using the REST API to browse the RC2 content from a java SE (desktop) client using the Apache http client library. I am using the simple HTTP approach for now -- no json yet.

Issue:

* I can login successfully (httppost), and I receive my own API key back. Bravo.
* I get a 403 (forbidden) error on subsequent "item" requests, regardless of the content of the headers and regardless of the content of the URL after "rest".
* If I turn off the REST module, I get a 500 error on the login.
* My host is SiteGround, and I did the RC2 install myself last week.

Java code snippets:

// LOGIN - WORKS FINE (requesting a REST API key)
httppost = new HttpPost("http://<bla>/~stillwa9/gallery3/index.php/rest");

nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("X-Gallery-Request-Method", "post"));
nvps.add(new BasicNameValuePair(
nvps.add(new BasicNameValuePair(

httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httppost);
System.out.println("Status: " + response.getStatusLine());

// Get item "1"
// THIS POST is getting a 403 regardless of the header values supplied, and regardless of the
// content of the URL past "rest". Garbage typed in place of "item" still gets a 403.
// I pass the request key from the login along in the X-Gallery-Request-Key header
// I have NO json code involved here yet.

String url = new String("http://<bla>/~stillwa9/gallery3/index.php/rest/item/1");
httppost = new HttpPost(url);
System.out.println("URL: " + url);

nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("X-Gallery-Request-Method", "get"));
nvps.add(new BasicNameValuePair("X-Gallery-Request-Key", RequestKey));
System.out.println("Headers: "+nvps.size());
System.out.println("Key: " + RequestKey);
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httppost);
System.out.println("Status: " + response.getStatusLine());

OUTPUT

Status: HTTP/1.1 200 OK
(key value in quotes)
URL: http://<bla>/~stillwa9/gallery3/index.php/rest/item/1
Headers: 2
Key: (key value without quotes)
Status: HTTP/1.1 403 Forbidden

Suggestions? Thanks in advance.

-- Dick

 
heiv

Joined: 2009-11-23
Posts: 43
Posted: Wed, 2010-08-18 03:54

Hi DickMcMullen.

Is there a HttpGet for the GET HTTP Method ?

I Used jersey for some tests and I didn't have problem.


Plugin Wordpress / Gallery 3 => http://www.heiv.fr/projets/plugin-wordpress-gallery-3/

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7985
Posted: Wed, 2010-08-18 05:01

You're very close! A couple of things:
1) You want to use GET not POST for the request for /rest/item/1 since you're looking up its data, not changing it
2) You want to set headers on the request, not POST form parameters.

I wrote up some demo Java code based on what you've done and put it here:
http://github.com/gallery/gallery3-contrib/tree/master/client/Java/

It works. Try it out.
---
Problems? Check gallery3/var/logs
bugs/feature req's | upgrade to the latest code | use git

 
DickMcMullen
DickMcMullen's picture

Joined: 2010-08-17
Posts: 21
Posted: Wed, 2010-08-18 11:11

Excellent! Thanks for the example with GSON. Yes, it is working fine using the get.

I was unsure of what was meant by this comment in the doc:

"REST uses the GET, POST, PUT and DELETE verbs as part of the HTTP protocol. However since many Apache servers don't support the HTTP PUT and DELETE actions in the default configuration, we rely on HTTP POST and pass the real verb in the X-Gallery-Request-Method header."

I was thinking that this comment applied to all verbs, so I was trying the post.

Thanks again for the quick response. I'm excited about G3...

-- Dick

 
heiv

Joined: 2009-11-23
Posts: 43
Posted: Wed, 2010-08-18 11:24

@DickMcMullen : what are your projects with java and gallery3 ?


Plugin Wordpress / Gallery 3 => http://www.heiv.fr/projets/plugin-wordpress-gallery-3/

 
DickMcMullen
DickMcMullen's picture

Joined: 2010-08-17
Posts: 21
Posted: Wed, 2010-08-18 14:55

@heiv: I am working on a 100% Java/Swing desktop app that will push/sync large collections to G3, with recursive directory support. I have a large collection and it is growing, so a tool that handles large collections would be very valuable to me.

I am pumped on the REST capabilities of G3, because of the ability to effectively sync changes from a local collection. Examples:
* Push new directories of photos (recursively), via drag-n-drop.
* Detect new local photos/directories automatically, and push them en-masse.
* "Hide/delete these existing images", and have the tool pull them off of G3 in real-time.
* Other attributes could be applied locally and replicated to G3 as well.

There is a sync tool for Flickr that does some of this, and I almost moved to Flickr because of that tool, but Flickr is just too annoying, and Gallery is a better product, at least for my purposes.

I am new to swing so this is a learning exercise for me. I am currently modeling the UI after Filezilla, the FTP client.

Not sure how much time I will have to dedicate to this, but I want a tool badly, so I'm giving it a shot.

-- Dick

 
heiv

Joined: 2009-11-23
Posts: 43
Posted: Wed, 2010-08-18 15:15

@DickMcMullen : Hey, it will be cool :)
I begun a tool like you describe, but I stopped 2 weeks ago because I didn't have much time to code ... with my holidays the next week, I prefered to stop.

Like you, it is a way to discover and use Swing.


Plugin Wordpress / Gallery 3 => http://www.heiv.fr/projets/plugin-wordpress-gallery-3/

 
feldi88

Joined: 2010-09-18
Posts: 2
Posted: Mon, 2010-09-20 05:17

Hi everyone,
I'm also trying to implement some stuff using the Rest API with Java.
With getting the gallery data, I'm fine, but I got problems to upload new photos. I use the HTTP Post and can do everything with it like creating new albums and so on. But I don't know how to upload the binary data. I've tried to submit the data with the other enity things without success.
Can someone post a small piece of code how I can upload a photo or expand the code demo at http://github.com/gallery/gallery3-contrib/tree/master/client/Java/ ?
Thank you!

 
rak

Joined: 2010-08-09
Posts: 44
Posted: Mon, 2010-09-20 11:21

this shows the data that needs to be sent.

and here is an example in python. If I get some time later i'll look at the java implementation.

 
feldi88

Joined: 2010-09-18
Posts: 2
Posted: Mon, 2010-09-20 19:12

Thank you very much! With your hint I have uploaded the first photos with the REST api.
I'm very happy and satisfied with Gallery3 and especially with its API, it will help me a lot :-)