[英]Facebook API: How to publish to Page feed while user is offline without offline_access permission
使用Facebook的Graph API,我成功地在用戶離線時發布到用戶的Feed,只有publish_stream權限。 我不需要offline_access權限。 我在這里解釋了我是如何做到的: Facebook Stream在離線時發布
當用戶離開時,我沒有成功發布到頁面。 這是我的情景:
用戶U是Page P的管理員.P授權並安裝我的應用程序。 U授權我的應用程序並授予我以下擴展權限:
一旦用戶離線,如果我嘗試使用相同的技術發布到用戶的流(沒有offline_access權限)而是發布到頁面,我得到“用戶沒有授權應用程序執行此操作”。 這是技術:
1)獲取我的應用程序的access_token
2)使用我的應用程序的access_token發布到Page P的提要: POST https://graph.facebook.com/ {page_id} / feed
如果在步驟2中使用{user_id}代替{page_id},那么它會毫無問題地發布到用戶的Feed。 但是,我想發布到Page的Feed。 是否有可能做到這一點? 或者我是否需要用戶的offline_access權限才能執行此操作?
謝謝,約翰尼
全流程 - 四頁示例(更容易編輯和理解示例)
所以配置只有基本的應用信息......
當你加載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";
?>
問題是你需要使用訪問令牌為通過獲得的提交提供的頁面...
呃...更容易說出來是這樣的:
您的應用請求預先設置“manage_pages” - 一旦您接受/授予預授權,那么您有一個access_token用於應用程序預置(離線只會使expires = 0)
所以現在你的應用程序已預先管理你的頁面,但它需要特定頁面的令牌......
因此,如果您使用第一個令牌發出/me/accounts
(或/UID/accounts
),您將獲得應用程序可以訪問的頁面列表及其各自的令牌...
從那里只需獲取頁面的標記,然后使用該標記發出命令
使用facebook類( facebook.php
和cert文件)
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,然后我從該子陣列中獲取令牌...
這是我的答案。 上面的代碼對我不起作用。 但我為自己制作了一個,完美無缺。 這是代碼。
服務器端代碼:
<?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>
請從fb apps advance設置中禁用“刪除offline_access權限:”。 通過選擇禁用單選按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.