/* * posts an image to the users news feed * @param message to show * @param image as form data * @return the new image id if successful */ public String publishPicture(String msg, Image image, String placeId) throws IOException { OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.facebook.com/v2.2/me/photos"); // request node request.addHeader("Authorization", "Bearer " + accessTokenString); // authentificate // check input to avoid error responses if (msg != null && image != null) { // facebook requires multipart post structure MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addTextBody("message", msg); // description if (placeId != null && !"".equals(placeId)) { builder.addTextBody( "place", placeId); // add link to FabLab site if property is set in preferences } // convert image to bytearray and append to multipart BufferedImage bimage = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics2D bGr = bimage.createGraphics(); bGr.drawImage(image, 0, 0, null); bGr.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bimage, "png", baos); builder.addBinaryBody(msg, baos.toByteArray(), ContentType.MULTIPART_FORM_DATA, "test.png"); // generate multipart byte stream and add to payload of post package HttpEntity multipart = builder.build(); ByteArrayOutputStream multipartOutStream = new ByteArrayOutputStream((int) multipart.getContentLength()); multipart.writeTo(multipartOutStream); request.addPayload(multipartOutStream.toByteArray()); // set header of post package Header contentType = multipart.getContentType(); request.addHeader(contentType.getName(), contentType.getValue()); // send and response answer Response response = request.send(); return response.getBody(); } else { throw new RuntimeException(CONSTANTS.get(FACEBOOK_MESSAGE_IMG_NEEDED)); } }
/** * Call api endpoint * * @param verb http-method to use, like: GET, POST, PUT, DELETE, PATCH * @param url the api-url to call * @return the output of the api-call, can be a JSON-string */ private String call(Verb verb, String url) { String urlEnd = url; if (!url.startsWith("/")) { urlEnd = "/" + url; } OAuthRequest request = new OAuthRequest(verb, "https://graph.facebook.com/v2.2" + urlEnd); request.addHeader("Authorization", "Bearer " + accessTokenString); Response response = request.send(); return response.getBody(); }