Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

Using FaceBook JavaScript SDK to call their Graph API

I am trying to expand my experience and get out of my comfort zone, so I am trying to integrate FB API into my website. I was wondering if there is any good literature to read that can give me a good understanding on how to build this into my website. I am looking to pull pictures from my FB into my web app.

2 Answers

Caesar,

I also have thought that pulling info from Facebook would be a fun project to try for a green coder. Some things to consider...

  1. Facebook's graph API for the most part requires you to have more than a website, it requires you to have hosting that a backend can run on.

  2. Using Facebook is within your grasp, but be warned that it might push a beginners skills. There's a lot of time sensitive authentication passing that goes on, and the documentation is thick.

That said, I have done it before, and have actually done exactly what you're asking for with a static site. Using the graph API, like I said, requires a backend hosting platform, just so you know. You're going to have to register as a facebook developer app at https://developers.facebook.com/. There's instructions for a lot of different languages, and Facebook has it's own graph API wrapper they want you to use. Don't ask me about the wrapper, because I've never used it. There's nothing special about it compared to good old get/post requests off of a REST API. But it does automate some of the authentication for you. There's also a practice terminal at https://developers.facebook.com/tools/explorer/ to make practice get and post requests, but again, to see that, register as a developer. Find out if your hosting can support a backend and get back to me if you need more instructions, I really is fun and I know a few of the in's and out's now.

Nicolas

Thank you so much for the insight on this matter, and I will be sure to reach out to once I get this project started.

Joshua Hawthorne
Joshua Hawthorne
18,523 Points

Has anyone seen any tutorials for these API's online? I'm trying to execute the most basic of API requests with PHP SDK but I keep getting errors. The Facebook documentation seems to be lacking in detail and assumes you know a lot of things (which I'm sure most of us newbies on Treehouse do not). For example, a simple GET me request keeps returning an error about appsecret_proof, even though I do not have appsecret_proof enabled. Where can we go for help BESIDES the documentation?

Are you sending an AuthO token with the get request? If you're not using facebook's SDK and sending a simple get request, they're going to need an AuthO token attached to the end of the query, usually created in the last sixty minutes.

Joshua Hawthorne
Joshua Hawthorne
18,523 Points

I have an access token I'm using from the Facebook Graph API Explorer (is that the correct token?). I am using the PHP SDK. My code is below:

$fb = new Facebook\Facebook([
        'app_id' => $app_id,
        'app_secret' => $app_secret, 
        'default_graph_version' => 'v2.4', 
        'default_access_token' => $access_token,  //copied and pasted from facebook graph API explorer  
        ]);

try {
           $response = $fb->get('/me');
           var_dump($response);
    } catch (Exception $e) {
        echo "Caught exception " . $e->getMessage();
        }