Sergio Arcos Hurtado
asked this on July 30, 2011 15:46
Hi,
I´m trying to upload a file using the information available on Layar Publishing API but I´m experiencing some problems. Here is my code snippet:
OAuthRequest request2 = new OAuthRequest(Verb.POST, "http://pub-api.layar.com/api/layers/16313/");
request2.addHeader("Content-Type", "multipart/form-data");
request2.addBodyParameter("banner_icon_high", "http:\\/\\/93.189.90.144\\/1\\/HOTSPOT\\/LAYAR\\/OBJETO2D\\/1G.png");
FileInputStream fin = null;
try {
File file = new File("C:\\Documents and Settings\\Administrador\\Escritorio\\TEST_LOGOS\\120x52.PNG");
byte fileContent[] = new byte[(int)file.length()];
fin = new FileInputStream(file);
fin.read(fileContent);
request2.addPayload(fileContent);
service.signRequest(accessToken, request2);
Response responseupdate2 = request2.send();
} catch(Exception ex) {
System.out.println(">> " + ex.getMessage());
} finally {
fin.close();
}
It´s pretty simple but I´m getting "401 - UNAUTHORIZED" Error ¿? ... before this I´ve the same code changing the "banner_text_color" property for the same layer and works OK. I´ve uploaded the example file.
There is way to know more exactly what is causing the error? Some logs or anything?...
Thanks and best regards,
Sergio
Comments latest first
Finally I get a solution changing the OAuth and the Http Client library (using "signpost-*" and "httpclient" respectively). I post the code I´ve used:
HttpPost request = new HttpPost(url_to_update_layer);
FileBody bin = new FileBody(new File(filePath));
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("icon_high", bin);
request.setEntity(reqEntity);
consumer.sign(request);
HttpResponse response = httpClient.execute(request);
HttpEntity result = response.getEntity();
Perhaps I missed on help documentation about "Layar Connect" an explanation about that the key of the image field you want to upload (ex. "icon_high") has to be the key of the binary data on "multipart/form-data" body.
Thanks and best regard,
Sergio