Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Cover Photo using Facebook API

In my Android application, I am trying to get the cover photo of the user from his Facebook account.

I can get the profile picture by using the below code.

profilePicUrl = new URL("http://graph.facebook.com/" + userId + "/picture?type=large");

profilePicBmp = BitmapFactory.decodeStream(profilePicUrl.openConnection().getInputStream());

The documentation specifies the following for retrieving the cover photo.

The user's cover photo (must be explicitly requested using fields=cover parameter)

Requires access_token

Returns : array of fields id, source, and offset_y

So, the structure of the JSON response would be something like this.

{
   "cover": {
      "cover_id": "10151008748223553",
      "source": "http://sphotos-a.ak.fbcdn.net/hphotos-ak-ash4/s720x720/391237_10151008748223553_422785532_n.jpg",
      "offset_y": 0
   },
   "id": "19292868552"
}

I am pretty new to Facebook Graph API and hence do not have much knowledge on how to go about this.

I tried this coverPicUrl = new URL("http://graph.facebook.com/" + userId + "/cover?type=large");

and also this coverPicUrl = new URL("http://graph.facebook.com/" + userId + "/fields=cover");

But I have not been able to get the cover picture of the user profile.

Searching online also did not yield any fruitful results.

Any help would indeed be appreciated.

Thanks!

like image 421
Swayam Avatar asked Sep 15 '12 03:09

Swayam


People also ask

How can I download Facebook cover photo?

The cover photo is at the top of the page and will open in the image viewer. Right-click the image. A menu will appear on your cursor. Click Save image as….

Can I use Facebook API for free?

In the newest version of the Graph API (v2. 9), we're announcing features that simplify development, making it even easier to build apps and experiences with Facebook. We're providing free access to over 140 million places around the world, the same data that powers Facebook, Instagram, and Messenger.

Does Facebook have a public API?

With the Facebook API, you can access and read public data for Facebook Pages that you are not the admin of. This data, including business metadata, public comments, and posts, can be used for competitive analysis and benchmarking.


2 Answers

The "source" tag (JSONObject) is nested inside another JSONObject, the "cover" tag. To parse this result, you will have to use something like this:

JSONObject JOSource = JOCover.optJSONObject("cover");
String coverPhoto = JOSource.getString("source");

The JOCover used in the example assumes that you already have a JSONOBject (JOCover) to parse the root. You can substitute your own JSONObject in its place.

The "source" tag cannot be accessed directly as it is nested in the "cover" tag. You will have to use ".optJSONObject("cover")". I have seen people use .getString instead of the .optJSONObject but I have never used it. Choose what works for you.

EDIT

As per your request for a solution using Graph API, I am editing the earlier solution and replacing it with the Graph API solution.

Preferably, in an AsyncTask, use this code in the doInBackground:

String URL = "https://graph.facebook.com/" + THE_USER_ID + "?fields=cover&access_token=" + Utility.mFacebook.getAccessToken();

String finalCoverPhoto;

try {

    HttpClient hc = new DefaultHttpClient();
    HttpGet get = new HttpGet(URL);
    HttpResponse rp = hc.execute(get);

    if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String result = EntityUtils.toString(rp.getEntity());

        JSONObject JODetails = new JSONObject(result);

        if (JODetails.has("cover")) {
            String getInitialCover = JODetails.getString("cover");

            if (getInitialCover.equals("null")) {
                finalCoverPhoto = null;
        } else {
            JSONObject JOCover = JODetails.optJSONObject("cover");

            if (JOCover.has("source"))  {
                finalCoverPhoto = JOCover.getString("source");
            } else {
                finalCoverPhoto = null;
            }
        }
    } else {
        finalCoverPhoto = null;
    }
} catch (Exception e) {
    // TODO: handle exception
}

I have tested this solution and works perfectly. You will have to add any addition fields to the base URL that are required for your activity. For the sake of testing, I used just the fields=cover

And in the onPostExecute, do your thing to display the cover picture. Hope this helps.

like image 133
Siddharth Lele Avatar answered Oct 19 '22 06:10

Siddharth Lele


Note: Fetching Cover Photo using Facebook API and endpoint https://graph.facebook.com/me?fields=cover no longer works as on 20th Dec 2014.

It was supposed to give following response:

{
   "cover": {
   "cover_id": "10151008748223553",
   "source": "http://sphotos-a.ak.fbcdn.net/hphotos-ak-ash4/s720x720/391237_10151008748223553_422785532_n.jpg",
   "offset_y": 0
   },
   "id": "19292868552"
}

But now it just gives User's id:

{
  "id": "19292868552"
}

Verified this using Graph Tool explorer 2.2 using me?fields=cover.

like image 37
Raghu Avatar answered Oct 19 '22 05:10

Raghu