I am trying to use the following code to upload photos to Facebook using the Graph API. I keep getting a βbad requestβ, but I donβt know why. I can upload a photo using curl with the same options. I am using Java with HttpClient.
PostMethod filePost = new PostMethod('https://graph.facebook.com/me/photos'); filePost.setParameter('access_token', 'my-access-token') filePost.setParameter('message', 'test image') filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); try { println("Uploading " + file.getName() + " to 'https://graph.facebook.com/me/photos'"); Part[] parts = [new FilePart('source', file.getName(), file)] filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); int status = client.executeMethod(filePost); if (status == HttpStatus.SC_OK) { println( "Upload complete, response=" + filePost.getResponseBodyAsString() ); } else { println( "Upload failed, response=" + HttpStatus.getStatusText(status) ); } } catch (Exception ex) { println("ERROR: " + ex.getClass().getName() + " " + ex.getMessage()); ex.printStackTrace(); } finally { filePost.releaseConnection(); }
UPDATE: More. I grabbed more information about this answer, and I get the following:
{"error": {"type": "OAuthException", "message": "An active access token should be used to request information about the current user." }}
But this does not seem to be correct, since I use the access token, which facebook returns to me after the authorization process.
Curl working code:
curl -F 'access_token=my-access-token' -F ' source=@ /path/to/image.jpg' -F 'message=Some caption' https:
Gregg source share