Reputation: 15
I would like to know how can I get an access token easily for my Facebook app? I'm new to this so a step by step explanation would be appreciated. I intend on making an app which displays user's data such as name, bio, dob and email. Moreover the app also shows the feed from a page. Using the page's id/name or category. So far I have this. Somethings might be extra here as I was trying to experiment.
//Loading the SDK
require_once("facebook.php");
//Setting up APP parameters
$config = array();
$config['appId'] = 'xxxxxxxxxxxx';
$config['secret'] = 'xxxxxxxxxxxx';
$facebook = new Facebook($config);
$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
// Getting the user id
$user = $facebook->getUser();
$user_me = $facebook->api('/me','GET');
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$params = array(
'scope' => 'read_stream, user_about_me',
'redirect_uri' => 'http://fblabs.chimpitup.com/'
);
$loginUrl = $facebook->getLoginUrl($params);
}
Upvotes: 0
Views: 7044
Reputation: 19995
If you are using the PHP SDK then you don't need to know the access token explicitly. You also don't need to get or set it using getAccessToken
and setAccessToken
. All of this is handled by the user clicking on the link generated from $facebook->getLoginUrl($params);
and then the function getUser
.
So remove
$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
Also the following line will trigger an error since it's not in a try-catch and it's redundant with the next few lines.
$user_me = $facebook->api('/me','GET');
After knowing you have authenticated with this
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
You can then make a call with a conditional
if ($user) {
$page_posts = $facebook->api('/PAGE_ID/posts');
}
Where PAGE_ID
is the id of the page you are looking to get the feed from
$page_posts
is a collection of post objects that can be accessed by looping through
foreach($page_posts['data'] as $post){
$post_link = $post['actions'][0]['link'];
$page_id = $post['from']['id'];
$page_name = $post['from']['name'];
$message = ($post['message']) ? $post['message'] : " ";
$name = ($post['name']) ? $post['name'] : " ";
$story = ($post['story']) ? $post['story'] : " ";
$post_time = $post['updated_time'];
}
For example displaying it in HTML
<div id="stream">
<?php foreach($user_posts['data'] as $post){
$post_link = $post['actions'][0]['link'];
$page_id = $post['from']['id'];
$page_name = $post['from']['name'];
$message = ($post['message']) ? $post['message'] : " ";
$name = ($post['name']) ? $post['name'] : " ";
$story = ($post['story']) ? $post['story'] : " ";
$post_time = $post['updated_time'];
?>
<div class="post">
<div class="picture">
<a href="http://facebook.com/<?php echo $page_id; ?>"><img src="http://graph.facebook.com/<?php echo $page_id; ?>/picture?type=square"/></a>
</div>
<div class="body">
<a href="http://facebook.com/<?php echo $page_id; ?>" class="actor"><?php echo $page_name; ?></a>
<span class="message"><?php echo $message; ?></span><br>
<span class="message"><?php echo $name; ?></span><br>
<span class="message"><?php echo $story; ?></span>
<div class="meta">
<a href="<?php echo $post_link ?>" class="permalink"><?php echo date("F j g:i a", strtotime($post_time)); ?></a>
</div>
</div>
</div>
<?php } ?>
</div><!-- end #stream -->
See https://developers.facebook.com/tools/explorer/?method=GET&path=facebook%2Fposts for an example of how a page posts is made up and https://developers.facebook.com/docs/reference/api/post/ to know how a post object is made
See the example https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php and PHP Getting Started Section https://developers.facebook.com/docs/php/gettingstarted/ and where some of the code in this answer was sourced https://github.com/phwd/hellopageposts/blob/master/index.php
Upvotes: 4