-1

I simply need to get the newest post from a specific Facebook page as a string in JavaScript for a website. I have never used the Facebook API and I'm still new to JavaScript, after many attempts I just can't figure out what the problem is...

So far this is everything I've done:

  • Created a Facebook app, chose 'website' for the platform
  • Inserted the JavaScript SDK code that I got from Facebook into my website code and replaced the placeholder 'appID' with the 'App ID' from my Facebook app
  • Became admin to the Facebook Page
  • On the Graph API Explorer page used the 'Get Token' dropdown to select 'Get Page Access Token', then selected the Facebook Page that shows up in the dropdown
  • Used the following request in the Graph API Explorer: 'https://www.facebook.com/pagenamehere/?fields=posts.limit(1)' (The output provides the post I need but with some extra information like "create_time", "id", etc. that I will most likely remove in JavaScript unless there is a more precise request I can use instead since I only need "message")
  • Copied the provided JavaScript code from 'Get Code' and inserted this under the JavaScript SDK code
  • replaced '//Insert your code here' with 'alert (response)', the alert message is: [object Object]

Either I don't know how to obtain "message" from '[object Object]', or I'm not getting the response I need. I've tried to do this with the webpage on a live server.

Please help, I'm out of ideas and I've read the Facebook API too many times.

Thanks in advance.

Shoey
  • 1
  • 2

2 Answers2

0

It's simple. Just have a look at the Graph API Explorer, and you'll get the idea how to access the message property:

A call to /buzzfeed/feed?limit=1 will return

{
  "data": [
    {
      "message": "science",
      "created_time": "2015-11-23T07:01:00+0000",
      "id": "21898300328_10154109572045329"
    }
  ],
  "paging": {
    "previous": "https://graph.facebook.com/v2.5/21898300328/feed?limit=1&format=json&since=1448262060&access_token=&__paging_token=&__previous=1",
    "next": "https://graph.facebook.com/v2.5/21898300328/feed?limit=1&format=json&access_token=&until=1448262060&__paging_token="
  }
}

which means that

console.log(JSON.stringify(response.data.message));

should print you the message.

Tobi
  • 31,405
  • 8
  • 58
  • 90
0

Thank you for your suggestions, they helped point me in the right direction and I was able to get this to work.

Here are the missing steps from my original post required to make it work:

  • Had to also add my domain to the 'Valid OAuth redirect URIs' field in 'My Apps' -> 'Settings' -> 'Advanced'

  • My access token had to be included in the code generated from the Graph API Explorer, like the answer in this post: (your user access token should be extended to 60 days in the 'Access Token Tool' page by clicking 'debug' -> 'Extend Access Token' and using this token in the code)

  • The Facebook SDK needs to finish loading before the code generated from the Graph API Explorer is executed, I used Mohammed Arif's answer from this question to do this but I had to use a delay for it to work. I put his code in a function that runs after a 200ms setTimeout, if it is loaded the code generated from the Graph API Explorer executes, else I run the function after a 200ms setTimeout again.

  • Tobi's answer to part of the question (to get the message) was almost correct: response.data[0].message;

Community
  • 1
  • 1
Shoey
  • 1
  • 2