Uploading Images with Java

939 views
Skip to first unread message

Jerry Cheslow

unread,
Jun 24, 2014, 4:02:02 PM6/24/14
to etsy-...@googlegroups.com
Hi,

I'm trying to make a simple Java application that will upload items for me, and I have nearly everything working. The one part I'm having a problem with is adding images; no matter what I try, I get some form of error. All the examples for image uploading to Etsy I've found are in PHP or other languages that I'm not familiar with, and as this is my first time doing something like this, the documentation on multipart form uploads is more confusing than helpful, so I felt it would be more productive to just ask for help rather than keep throwing things at the proverbial wall.
I'm using the EtsyAndroidSDK library, which includes a ListingImagesRequest.uploadListingImage(String listingID) method; however, all this does is create the url for the method call rather than let me pass an image. If anyone could provide an example of working Java code to upload an image (either during item creation or after the fact), I think I would be able to incorporate it into my code and handle the rest from there.

Thanks,
Jerry

Ian Adie

unread,
Jun 25, 2014, 4:00:21 AM6/25/14
to etsy-...@googlegroups.com
Hi Jerry,

Here's a code snippet of how I upload images, in Android:

String url = Oauth.URL + "/listings/"+listingID+"/images";

HttpPost request = new HttpPost(url);

mConsumer.sign(request);

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
File file = new File(c.getFilesDir().toString()+"/photo.jpg");
FileBody imageBody = new FileBody(file, "image/jpeg");

entity.addPart("image", imageBody);
request.setEntity(entity);

HttpClient httpClient = Oauth.getSecureClient();
org.apache.http.HttpResponse response = httpClient.execute(request);

BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String reply = in.readLine();
Log.d(LOG_TAG, reply);

and getSecureClient method:

public static HttpClient getSecureClient() {
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
HttpParams params = new BasicHttpParams();
SingleClientConnManager mgr = new SingleClientConnManager(params, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(mgr, params);
return httpClient;
}

I use the signpost library to handle the OAuth bits.

Hope this helps!

Ian

Jerry Cheslow

unread,
Jun 25, 2014, 2:14:14 PM6/25/14
to etsy-...@googlegroups.com
Hi Ian,

Thanks for the code, this look looks like what I need. However, the EtsyAndroidSDK library I'm using uses the Scribe library to handle OAuth, and I don't think I can make it use Signpost instead without breaking something important. I'm currently trying to perform an image upload with the code you posted, but doing so results in the usual "Image file or image_id string must be included" error, which makes me think I'm missing something important. It looks like I'm going to have to either start from scratch without the EtsyAndroidSDK library so that I can use Signpost or figure out a way to make image uploading work through Scribe, but I'm not having much success in either one. I've never worked with OAuth before this, so I'd rather find a way to make it work with the code I already have that uploads items without images. If you're able to help with that, or figure out why Signpost isn't working for me, that would be incredibly helpful.

Here's what I have so far:




OAuthConsumer consumer = new CommonsHttpOAuthConsumer(KEY, SECRET);
OAuthProvider provider = new CommonsHttpOAuthProvider(
 
 
String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
 
Desktop d = Desktop.getDesktop();
try{
d.browse(new URI(authUrl));
}
catch (Exception e){
e.printStackTrace();
}
Scanner s = new Scanner(System.in);
System.out.println("Type in the authentication code from Etsy.");
String auth_code = s.nextLine();
s.close();
provider.retrieveAccessToken(consumer, auth_code);
 
String url = "https://openapi.etsy.com/v2/listings/"+LISTING_ID+"/images";

HttpPost request = new HttpPost(url);

consumer.sign(request);

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
File file = new File(filePath);
FileBody imageBody = new FileBody(file, "image/jpeg");

entity.addPart("image", imageBody);
request.setEntity(entity);
        
HttpClient httpClient = getSecureClient();
org.apache.http.HttpResponse response = httpClient.execute(request);
        
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String reply = in.readLine();
System.out.println(reply);


Thanks,
Jerry

Jerry Cheslow

unread,
Jun 26, 2014, 1:39:15 PM6/26/14
to etsy-...@googlegroups.com
I've solved the problem; I was practicing on a draft listing, which you apparently can't upload images to. Your code works perfectly when I try it on an actual listing, so thank you very much for your help.
Jerry.

Graeme Grant

unread,
Jun 26, 2014, 5:27:53 PM6/26/14
to EtsyAPI Forum, mark...@cybrtrayd.com
Actually, you can upload images to draft listings.
 
G.
 

Date: Thu, 26 Jun 2014 10:39:15 -0700
From: mark...@cybrtrayd.com
To: etsy-...@googlegroups.com
Subject: Re: Uploading Images with Java


I've solved the problem; I was practicing on a draft listing, which you apparently can't upload images to. Your code works perfectly when I try it on an actual listing, so thank you very much for your help.
Jerry.

--
You received this message because you are subscribed to the Google Groups "Etsy API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to etsy-api-v2...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jerry Cheslow

unread,
Jun 27, 2014, 10:17:59 AM6/27/14
to etsy-...@googlegroups.com, mark...@cybrtrayd.com
I'm sure there is a way, but my uploads started working as soon as I changed the listing ID from that of a draft to an active listing. Looking into why that happened is an issue for another time though, since now I'm getting a "restricted API key" error... Oh well, at least the upload worked that one time.

Graeme Grant

unread,
Jun 27, 2014, 3:05:14 PM6/27/14
to EtsyAPI Forum
It sounds like you API is not fully approved yet. You will need to contact developer[@]etsy[.]com to discuss off-group.
 
G.

 

Date: Fri, 27 Jun 2014 07:17:59 -0700
From: mark...@cybrtrayd.com
To: etsy-...@googlegroups.com
CC: mark...@cybrtrayd.com
Reply all
Reply to author
Forward
0 new messages