Hi John,

Thanks for this, seems to be heading in the right direction - although
I've now hit a weird issue with a 401 Unauthorised error.  At the
moment, I'm not sure where this is coming from as the accessor and
client have been set and other posts are working.  I changed the code
to below:

public static class InputStreamMessage extends OAuthMessage
    {
        public InputStreamMessage(String url, InputStream body,
List<Parameter> params) {
            super("POST", url, params);
            this.body = body;
        }

        private final InputStream body;

        @Override
        public InputStream getBodyAsStream() throws IOException {
            return body;
        }
    }

Also, I created a postImage function:

public OAuthMessage postImage(OAuthMessage request, ParameterStyle
params) {
                OAuthMessage return_value = null;
                try {
                        return_value = client.invoke(request, params);
                } catch (OAuthProblemException e) {
                        int status_code = e.getHttpStatusCode();
                    if (200 <= status_code && status_code < 300) {
                        // Not really a problem.
                    } else {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (OAuthException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return return_value;
        }

And this is how I invoke it:

try {
    OAuthMessage response = null;
        File input_file = new File(filename);
        InputStream input = new FileInputStream(input_file);
        OAuthMessage request = new InputStreamMessage(post_url, input,
OAuth.newList("photo[body]", post_message));
        request.getHeaders().add(new OAuth.Parameter("Content-Type", "image/
jpeg"));
        request.getHeaders().add(new OAuth.Parameter("Content-Length",
""+input_file.length()));
        response = oauth_request.postImage
(request,ParameterStyle.AUTHORIZATION_HEADER);
} catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

I had to add the Content-Length, as it was getting a 411 error that
the length was not set.  Any ideas why this would be getting
unauthorised (except the obvious answer that it's coming from the
other end, but I need to try eliminate anything at my end).

Thanks

On Jan 26, 10:57 pm, John Kristian <[email protected]> wrote:
> I suggest you try posting a file without using OAuth; that is using
> org.apache.http.client directly.  I tried it, and couldn't get it to
> work.  So there's something tricky about it that I don't understand.
>
> When using OAuth, I suggest you don't send the photo as an OAuth
> parameter.  Instead, send the photo as the HTTP request body, and send
> the OAuth parameters in an Authorization header or URL query string.
>
> net.oauth.client.httpclient4.HttpClient4 will add an InputStreamEntity
> to the request object if the method is POST or PUT and the request
> OAuthMessage has a body; that is if getBodyAsStream returns an
> InputStream (not null).  To trigger this behavior you can execute
> something like this:
>
>     void upload(OAuthClient client, String httpMethod, String url)
>         throws IOException, OAuthException
>     {
>         OAuthMessage response = null;
>         InputStream input = new FileInputStream("photo.jpg");
>         try {
>             OAuthMessage request = new InputStreamMessage(httpMethod,
> url, input);
>             request.getHeaders().add(new OAuth.Parameter("Content-
> Type", "image/jpeg"));
>             response = client.invoke(request,
> ParameterStyle.AUTHORIZATION_HEADER);
>         } finally {
>             input.close();
>         }
>     }
>
>     private static class InputStreamMessage extends OAuthMessage
>     {
>         InputStreamMessage(String method, String url, InputStream
> body) {
>             super(method, url, null);
>             this.body = body;
>         }
>
>         private final InputStream body;
>
>         @Override
>         public InputStream getBodyAsStream() throws IOException {
>             return body;
>         }
>     }
>
> On Jan 26, 12:15 pm, Tane Piper <[email protected]> wrote:
>
> > I'm now at the stage that I want to add photo posting to my app,
> > however I am having some issues.
>
> > I've got the fully qualified path to the file, and I just tried this
> > off hand:
>
> > params = OAuth.newList("photo[body]", post_message, "photo[photo]",
> > filename);
>
> > However I didn't expect this to work, all I get is a 500 code back.
>
> > I found an example of posting files using standard Java here:
> >http://www.anddev.org/solved_http-post_request_to_restful_rails_web-a...
> > but I cannot find how to add a InputStreamEntity to the parameters and
> > how to change the headers to support posting a file.
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"OAuth" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/oauth?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to