Posting photos to Facebook album using Graph API

28 / Jun / 2012 by Vishal Sahu 0 comments

In my recent grails project, i needed to post images to the albums for Facebook Profile/ Fan page/ Group using facebook API. I searched a lot and find a way to achieve this using Facebook Graph API.

To post any content over facebook, we need to have facebook access_token which can be obtained using steps described here

After successfully obtaining access_token, we can easily use graph API to post message/photos to any album for that profile or page.
We need to have album_id to post photos to any album. If there is no album present, the it will create an album with the same name as of the Facebook App, which is posting the photos and put all the photos in that Album.

Make sure the access_token have publish_stream permission to post the content over facebook.

Code to post photos to facebook album is as :-


String access_token= ACCESS_TOKEN // access_token obtained from facebook
String message= MESSAGE // message to be published with photo.
String photo_url=PHOTO_URL // url of the photo to be published

StringBuilder sb = new StringBuilder("access_token=");
sb.append(URLEncoder.encode(token, "UTF-8"));
sb.append(URLEncoder.encode(message, "UTF-8"));
sb.append(URLEncoder.encode(photo_url, "UTF-8"));
String feedUrl = ""
// use Album ID or FanPage ID in the feedURL instead of /me/ in case you want to publish photos to specific Album or Fan page

URL url = new URL(feedUrl);
HttpURLConnection connection
try {
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "multipart/form-data");
connection.setRequestProperty("Content-Length", "" + sb.toString().length());
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
catch (Exception e) {


This will post the provided image via url to the album mentioned in the API call.

This worked for me.
Hope it helps.

Useful Links:

Vishal Sahu


Leave a Reply

Your email address will not be published. Required fields are marked *