How to get permission?
I have create a simple facebook application. Now I want to get permission for read_stream.
I have read a lot of documentation but I don't understand.
Could you tell me how to get this permission with PHP or javascript ?
Edit:
I have used this source code ( you can se it working here ) but doesn't work because I get only basic permission:
<?php
require_once('facebook.php');
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if (!$me) {
$loginUrl = $facebook->getLoginUrl();
} else {
// $fbme is valid i.e. user can access our app
$logoutUrl = $facebook->getLogoutUrl();
}
// This call will always work since we are fetching public data.
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<!--
We use the JS SDK to provide a richer user experience. For more info,
look here: http://github.com/facebook/connect-js
-->
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId(); ?>',
session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to ac开发者_开发技巧cess the session
xfbml : true // parse XFBML
});
FB.login(function(response) {
if (response.session) {
if (response.perms) {
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
} else {
// user is logged in, but did not grant any permissions
}
} else {
// user is not logged in
}
}, {perms:'read_stream'});
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<h1>I love you</h1>
<?php if ($me): ?>
<a href="<?php echo $logoutUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
</a>
<?php else: ?>
<div>
Using JavaScript & XFBML: <fb:login-button></fb:login-button>
</div>
<?php endif ?>
<h3>Session</h3>
<?php if ($me): ?>
<pre><?php print_r($session); ?></pre>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $uid; ?>/picture">
<?php echo $me['name']; ?>
<h3>Your User Object</h3>
<pre><?php print_r($me); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>
var cb = function(response) {
//Log.info('FB.login callback', response);
if (response.status === 'connected') {
//Log.info('User logged in');
} else {
//Log.info('User is logged out');
}
};
FB.login(cb, {
scope: 'publish_stream',
enable_profile_selector: 1
});
references:
http://www.fbrell.com/saved/ac4c4abe0ea46d6363350b6f0c12c6ea http://www.softwareandfinance.com/apps/facebook/graph_api_access_feed_publish.html
// server-side to verify
$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
$result = $facebook->api($feed_dir, 'post', $msg_body);
}
// result is id of post
In javascript API you can request permissions during login using FB.login()
. More about it here (with examples)
In PHP you can request extended permission during authentication process. More about it here (see "Requesting Extended Permissions")
Javascript approach is much easier imo.
UPDATE
Ok there is the third way of getting permissions using fb:login-button
fbml tag:
<fb:login-button perms="read_stream,offline_access"></fb:login-button>
精彩评论