I suspect the server returns a 401 Unauthorised status because the request doesn't contain a valid oauth_signature and other OAuth parameters. Here's how to add those parameters:
OAuthAccessor accessor = ... OAuthMessage request = new InputStreamMessage(... request.addRequiredParameters(accessor); Sorry I gave a poor example. On Jan 27, 5:25 am, Tane Piper <[email protected]> wrote: > 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). --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
