Zum Inhalt springen

Facebook Graph API: Fehlende Felder einblenden

Die Facebook Graph API hat ihre kleine Besonderheiten. Eine davon sind die Felder. In der Graph API Reference sind diese Felder umfassend beschrieben. Bei der Arbeit mit der Graph API kommt es immer wieder dazu, dass Felder fehlen. Was ist hier zu beachten?

Um die versteckten Felder zu erhalten ist es notwendig bei dem Rest-Aufruf den Query-Parameter fields mitzugeben. Die Syntax ist eine komma-separierte Liste. Es ist zu bedenken, dass nur noch Felder übermittelt werden die angegeben wurden. D.h. die Angabe der fields ist nicht additiv, sondern selektiv.

Als Beispiel kann man diesen Aufruf betrachten https://graph.facebook.com/v2.3/cocacola

Das Ergebnis ist dann

{
  "id": "40796308305",
  "about": "The Coca-Cola Facebook Page is a collection of your stories showing how people from around the world have helped make Coke into what it is today.",
  "birthday": "05/08/1886",
  "can_post": true,
  "category": "Food/Beverages",
  "checkins": 13414,
  "cover": {
    "cover_id": "10152297032458306",
    "offset_x": 0,
    "offset_y": 0,
    "source": "https://scontent.xx.fbcdn.net/hphotos-prn2/v/t1.0-9/s720x720/625442_10152297032458306_574021701_n.jpg?oh=cac68b2a7ceed49f3e5a580ae52cfeaa&oe=561489E9",
    "id": "10152297032458306"
  },
  "description": "Created in 1886 in Atlanta, Georgia, by Dr. John S. Pemberton, Coca-Cola was first offered as a fountain beverage at Jacob's Pharmacy by mixing Coca-Cola syrup with carbonated water. 

Coca-Cola was patented in 1887, registered as a trademark in 1893 and by 1895 it was being sold in every state and territory in the United States. In 1899, The Coca-Cola Company began franchised bottling operations in the United States. 

Coca-Cola might owe its origins to the United States, but its popularity has made it truly universal. Today, you can find Coca-Cola in virtually every part of the world.

Coca-Cola Page House Rules: http://CokeURL.com/q28a",
  "founded": "1886",
  "global_brand_root_id": "1542920115951985",
  "has_added_app": false,
  "is_community_page": false,
  "is_published": true,
  "likes": 91406342,
  "link": "https://www.facebook.com/coca-cola",
  "name": "Coca-Cola",
  "parking": {
    "lot": 0,
    "street": 0,
    "valet": 0
  },
  "talking_about_count": 336137,
  "username": "coca-cola",
  "website": "http://www.coca-cola.com",
  "were_here_count": 0
}

Nutzt man nun den fields Parameter wird das Ergebnis erheblich eingeschränkt: https://graph.facebook.com/v2.3/cocacola

{
  "name": "Coca-Cola",
  "id": "40796308305"
}

Fields in Unterobjekten

Eine weitere Variante des fields-Parameter findet man bei Connections. Hierbei kann man zu einem Facebook Objekt weitere Unterobjekte erfragen. Dessen Felder kann man auch bereits bei der Anfrage eingrenzen.

Ein Beispiel hierfür ist folgender Post auf der Coca Cola-Facebook Fanpage. Es werden die Likes der Kommentare von Facebook abgerufen: https://graph.facebook.com/v2.3/231382263681424_497325003753814?fields=comments{like_count}

Das Ergebnis ist dann folgendes Objekt.

{
  "id": "231382263681424_497325003753814",
  "created_time": "2015-07-12T11:10:04+0000",
  "comments": {
    "data": [
      {
        "like_count": 1,
        "id": "497325003753814_497409610412020"
      },
      {
        "like_count": 2,
        "id": "497325003753814_497486827070965"
      },
      {
        "like_count": 1,
        "id": "497325003753814_497457423740572"
      },
      {
        "like_count": 1,
        "id": "497325003753814_497326780420303"
      },
      // ... a lot more comments ...
      {
        "like_count": 1,
        "id": "497325003753814_497417320411249"
      }
    ],
    "paging": {
      "cursors": {
        "after": "MTI=",
        "before": "MzY="
      },
      "next": "https://graph.facebook.com/231382263681424_497325003753814/comments?order=chronological&fields=like_count&limit=25&after=MTI%3D"
    }
  }
}

Fields und der Comment-Count

Ganz besonders interessant wird es dann, wenn man noch wissen möchte wiviele Comments ein Post hat. Hier gibt es auch noch eine relativ spezielle Syntax. Diese gibt nur den ersten Kommentar zurück und begrenzt das Paging auf 1 Element, aber liefert eine Zusammenfassung (Summary) mit. Das ganze sieht dann so aus:
https://graph.facebook.com/v2.4/231382263681424_497325003753814?fields=comments.limit(1).summary(true)

Das dazugehörige Ergebnis ist dann auch sehr überschaubar:

{
  "id": "231382263681424_497325003753814",
  "comments": {
    "data": [
      {
        "from": {
          "name": "Jüłïëń Jaquette",
          "id": "1078291755521220"
        },
        "message": "Ena to nom dans bagatelle mone trouver",
        "created_time": "2015-07-12T14:36:46+0000",
        "id": "497325003753814_497409610412020"
      }
    ],
    "paging": {
      "cursors": {
        "after": "NDI=",
        "before": "NDI="
      },
      "next": "https://graph.facebook.com/231382263681424_497325003753814/comments?order=chronological&limit=1&summary=true&after=NDI%3D"
    },
    "summary": {
      "order": "ranked",
      "total_count": 42,
      "can_comment": true
    }
  }
}

Hinweis: Insbesondere mit der neuen Version 2.4 der Graph API hat Facebook die Entscheidung getroffen nur noch wenige Felder vorzubefüllen. Dadurch wird der fields-Queryparameter in Zukunft immer wichtiger.

Published inAllgemein

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close