使用Facebook的Graph API,我在用户离线时仅使用publish_stream权限成功发布到用户的Feed。我不需要offline_access权限。我在这里解释了我是如何做到的:Facebook Stream Publish while offline
在用户离线时,我没有成功发布到网页。这是我的情景:
用户U是Page P. P的管理员,授权并安装我的应用。你授权我的应用并授予我以下扩展权限:
一旦用户离线,如果我尝试使用相同的技术发布到用户的流(没有offline_access权限)而是发布到页面,我得到“用户未授权应用程序执行此操作行动”。这是技术:
1)获取我的应用程序的access_token
2)使用我的应用程序的access_token发布到Page P的Feed: 发布 https://graph.facebook.com/ {page_id} / feed
如果在步骤2中使用{user_id}而不是{page_id},那么它会毫无问题地发布到用户的Feed中。但是,我想发布到Page的Feed。是否有可能做到这一点?或者我是否需要用户的offline_access权限才能执行此操作?
谢谢, 约翰尼
答案 0 :(得分:7)
全流程 - 四页示例(更容易编辑和理解示例)
所以配置只有基本的应用信息......
当您加载index.php
时,它会重定向到Facebook以获取页面的授权..(可能已经有了这个...但涵盖所有基础)
Facebook然后重定向回redirecturl(backfromfb.php
)...
Facebook将访问令牌作为哈希值而不是get变量返回,因此该页面将使用哈希值作为变量进行刷新...
然后加载PageUpdate.php
来处理通过app / admin令牌的循环,找到要发布到的页面的正确值。
然后它构建帖子并提交它..
听起来你已经理解了大部分内容......所以希望这会帮助你完成最后一点。
<强>的config.php 强>
<?php
$config = array(
'appId' => 'YOUR APP ID',
'secret' => 'YOUR APP SECRET',
'cookie' => true;
);
$baseurl = 'http://yoursite.com/';
$returnpage = 'backfromfb.php';
require_once('library/facebook.php');
?>
<强>的index.php 强>
<?php require_once('config.php'); ?>
<html><head><title>Redirecting for auth</title></head><body><script type="text/javascript">
window.location.href = 'https://www.facebook.com/dialog/oauth?client_id=<?php echo $config['appId']; ?>&redirect_uri=<?php echo $baseurl . $returnpage; ?>&scope=manage_pages&response_type=token';
</script></body></html>
<强> backfromfb.php 强>
<?php
require_once('config.php');
// this page just passes the access token from the hash to a GET arg...
if (!isset($_GET['access_token'])) {
?>
<html><head><title>Redirecting</title></head><body><script type="text/javascript">
accessToken = window.location.hash.substring(1);
window.location.href = '<?php echo $baseurl . $returnpage; ?>?' + accessToken;
</script></body></html>
<?php
} else {
require_once('PageUpdate.php');
} ?>
<强> PageUpdate.php 强>
<?php
require_once('config.php');
$pageID = "123456 WHatever you page id is";
$AppToken = array(
'access_token' => $_REQUEST['acess_token']
);
$fb = new Facebook($config);
// Load APP page access rights for user via AppToken
$pageAdmin = $fb->api('/me/accounts', 'GET', $AppToken);
// Find the page access token
foreach ($pageAdmin['data'] as $data) {
if ($data['id'] == $pageID) {
$pageToken['access_token'] = $data['access_token'];
continue;
}
}
// compile the post
$WallPost = array(
'message' => 'Test post from my app!'
); // you can also use 'picture', 'link', 'name', 'caption', 'description', 'source'....
//http://developers.facebook.com/docs/reference/api/
// post to wall
$response = $fb->api($pageID . '/feed','POST',$WallPost);
if($response) {
echo 'Success!';
echo '<pre>' . $response . '</pre>';
} else echo "failed";
?>
答案 1 :(得分:2)
问题是您需要使用通过获得的提交提供的页面的访问令牌...
呃......更容易说出来就是这样:
您的应用请求预先设置“manage_pages” - 一旦您接受/授予预先许可,那么您有一个access_token用于应用程序预置(离线只会使expires = 0)
所以现在您的应用已预先管理您的网页,但它需要特定网页的令牌...
因此,如果您使用第一个令牌发出/me/accounts
(或/UID/accounts
),您将获得应用程序可以访问的页面列表及其各自的令牌...
从那里抓取页面的标记然后发出带有该标记的命令
使用facebook类(facebook.php
和证书文件)
require_once 'library/facebook.php';
$app_id = "YOURAPPID";
$app_secret = "YOURSECRET";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$token = array(
'access_token' => 'THE Token from app / manage_page auth'
);
$userdata = $facebook->api('/me/accounts', 'GET', $token);
echo '<pre'>;
print_r($userdata);
echo '</pre>';
您应该会看到一个页面ID列表及其访问权限...
通常我做一个foreach $userdata['data']
并查找页面ID,然后我从该子阵列中获取令牌......
答案 2 :(得分:1)
这是我的答案。上面的代码对我不起作用。但我为自己制作了一个,完美无缺。这是代码。
服务器端代码:
<?php
@session_start();
require "fblib/facebook.php";
define('YOUR_APP_ID','');
define('YOUR_APP_SECRET','');
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
if($_SESSION['access_token']!='') {
$access_token = $_SESSION['access_token'];
$user_id = $_SESSION['user_id'];
} else {
$access_token = $_REQUEST['access_token'];
$_SESSION['access_token'] = $_REQUEST['access_token'];
$user_id = $_REQUEST['user_id'];
$_SESSION['user_id'] = $_REQUEST['user_id'];
}
$user_id = $_REQUEST['user_id'];
$facebook->setAccessToken($_REQUEST['access_token']);
$post = array(
'message' => 'This message is posted with access token - ' . date('Y-m-d H:i:s')
);
// and make the request
$response = $facebook->api('/me/feed', 'POST', $post);
?>
客户端代码:
<?php
require "fblib/facebook.php";
define('YOUR_APP_ID','387647494631464');
define('YOUR_APP_SECRET','857f41bdd23c26ae132a1c75a343ddc9');
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
// The access token we have is not valid
$user = null;
}
}
?>
<div id="fb-root"></div>
<script language="javascript" src="js/jquery-1.7.2.min.js"></script>
<script>
var accessToken;
var uid;
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement('script');
js.id = id;
js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
// Init the SDK upon load
window.fbAsyncInit = function() {
FB.init({
appId : '', // App ID
channelUrl : '//'+window.location.hostname+'/channel', // Path to your Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// listen for and handle auth.statusChange events
FB.Event.subscribe('auth.statusChange', function(response) {
if (response.authResponse) {
// user has auth'd your app and is logged into Facebook
FB.api('/me', function(me) {
if (me.name) {
// document.getElementById('auth-displayname').innerHTML = me.name;
accessToken = response.authResponse.accessToken;
uid = response.authResponse.userID;
}
})
// document.getElementById('auth-loggedout').style.display = 'none';
// document.getElementById('auth-loggedin').style.display = 'block';
} else {
// user has not auth'd your app, or is not logged into Facebook
// document.getElementById('auth-loggedout').style.display = 'block';
// document.getElementById('auth-loggedin').style.display = 'none';
}
});
// respond to clicks on the login and logout links
document.getElementById('auth-loginlink').addEventListener('click', function() {
// FB.login();
FB.login(function(response) {
// handle the response
}, {scope: 'offline_access,publish_stream'});
});
}
function gettoken() {
// alert("User Token :"+accessToken+", User id :"+uid);
$.post('fbpost.php',{access_token:accessToken,user_id:uid},function(data) {
// alert(data);
});
}
</script>
<?php if (!$user): ?>
<a href="Javascript:void(0)" id="auth-loginlink">Login with Facebook</a>
<?php else: ?>
<a href="Javascript:void(0)" id="auth-logoutlink" onClick="FB.logout()" >Logout from Facebook</a>
<?php endif ?>
<a href="Javascript:void(0)" onclick="gettoken()" >Post Into Wall</a>
请停用“删除offline_access权限:” 从fb应用程序提前设置。通过选择禁用单选按钮。