Hi Francisco, I have been trying to upload video/audio files using the code you shared but I am getting below error at line "conex.getInputStream()". However, when I checked the google photos album, the entry for this video is there but it says "Your video will be ready soon" for past 6-7 hours. Can you please advise if you faced this issue and help me fix this. Thanks.
ERROR: java.io.IOException: Server returned HTTP response code: 500 for URL: https://picasaweb.google.com/data/feed/api/user/default/albumid/default at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263) at oneflipkart.ServiceClient.postImage(ServiceClient.java:223) at oneflipkart.ServiceClient.main(ServiceClient.java:52) Exception in thread "main" java.lang.NullPointerException at oneflipkart.ServiceClient.postImage(ServiceClient.java:243) at oneflipkart.ServiceClient.main(ServiceClient.java:52) On Tuesday, 2 June 2015 23:51:36 UTC+5:30, Francisco Jiménez wrote: > > 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 https://groups.google.com/group/google-picasa-data-api. For more options, visit https://groups.google.com/d/optout.
