It's working now, after many hours. I even had to trace the HTTP request
with Wireshark, it helped me a lot!.
In my case the problem was at the last line of the request. After the image
data is written, I need to write a new empty line and finally the
"--END_OF_PART--".
My working code for Java:
public static String postImage(String url, String accessToken) {
HttpURLConnection conex = null;
OutputStream urlOut = null;
InputStream in = null;
URL serverUrl;
String picasaResponse = "";
String strBase =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
String boundary = "";
/*Randomly generate the boundary string */
for (int i = 0; i < 16; i++) {
int numero = (int) (Math.random() * (strBase.length()));
char caracter = strBase.charAt(numero);
boundary = boundary + caracter;
}
System.out.println("BOUNDARY:" + boundary);
try {
String imagePath =
"C:\\Users\\Dendral\\Pictures\\medusaSmall.jpg";
File fileImage = new File(imagePath);
StringBuilder writeData = new StringBuilder();
writeData.append("--").append(boundary).append("\r\n");
writeData.append("Content-Type: application/atom+xml;\r\n\r\n");
writeData.append("<entry
xmlns='http://www.w3.org/2005/Atom'>\r\n");
writeData.append("<title>plz-to-love-realcat.jpg</title>\r\n");
writeData.append("<summary>Real cat wants attention
too.</summary>\r\n");
writeData.append("<category
scheme=\"http://schemas.google.com/g/2005#kind\"\r\n");
writeData.append(
"term=\"http://schemas.google.com/photos/2007#photo\"/>\r\n");
writeData.append("</entry>\r\n");
writeData.append("--").append(boundary).append("\r\n");
writeData.append("Content-type: image/jpeg\r\n\r\n");
StringBuilder endOfPart = new StringBuilder();
endOfPart.append("\r\n--").append(boundary).append("--");
long totalLenght = writeData.length() + fileImage.length() +
endOfPart.length();
serverUrl = new URL(url);
conex = (HttpURLConnection) serverUrl.openConnection();
conex.setConnectTimeout(30000);
conex.setReadTimeout(60000);
conex.setRequestMethod("POST");
conex.setDoInput(true);
conex.setDoOutput(true);
conex.setUseCaches(false);
conex.setRequestProperty("Host", "picasaweb.google.com");
conex.setRequestProperty("GData-Version", "2");
conex.setRequestProperty("Content-Length", String.valueOf(
totalLenght));
conex.setRequestProperty("Content-Type", "multipart/related;
boundary=\"" + boundary + "\"");
conex.setRequestProperty("Authorization", "Bearer " +
accessToken);
conex.setRequestProperty("MIME-version", "1.0");
conex.connect();
urlOut = conex.getOutputStream();
DataOutputStream out = new DataOutputStream(urlOut);
out.write(writeData.toString().getBytes());
/*TODO: validate if file exists*/
FileInputStream fileStream = new FileInputStream(fileImage);
byte buf[] = new byte[1024];
int len = 0;
while (len >= 0) {
out.write(buf, 0, len);
len = fileStream.read(buf);
}
out.write(endOfPart.toString().getBytes());
out.flush();
in = conex.getInputStream();
picasaResponse = getResponse(in);
} catch (Exception ioe) {
System.out.println("ERROR:");
ioe.printStackTrace();
try {
picasaResponse = getResponse(conex.getErrorStream());
} catch (Exception e) {
/*Ignore*/
}
} finally {
close(urlOut);
close(in);
if (conex != null) {
conex.disconnect();
}
}
return picasaResponse;
}
postImage(
"https://picasaweb.google.com/data/feed/api/user/default/albumid/THE_ALBUM_ID"
, "YOUR_ACTIVE_ACCESS_TOKEN")
You can also play a little in the
https://developers.google.com/oauthplayground/, generate a "INSERT MEDIA"
request from the 'List of possible operations" and see the very details of
the request.
EOF
El martes, 2 de junio de 2015, 10:47:52 (UTC-5), Prerana Polekar escribió:
>
> Hi Francisco,
>
> Nope no progress on this issue :( I am also able to upload the image
> without metadata but badly stuck in upload with metadata! I think the issue
> is with the binary image data appened to the xml content. I am unable to
> figure out how to correctly append the image data along with xml
> information. Any help or pointers will be highly appreciated!!
>
>
>>
>>>
>>>
--
You received this message because you are subscribed to the Google Groups
"Google Picasa Web Albums API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-picasa-data-api.
For more options, visit https://groups.google.com/d/optout.