我有两个页面,facebook.php和generatePageToken.php我试图将页面发布到我的Facebook页面,但此脚本目前仅作为管理员发布。在facebook.php页面中,我尝试获取访问令牌,但它不起作用 - 它警告回空白。请不要告诉我阅读一些API,我已经阅读了所有内容,而且我真的不明白这有什么问题。如果有人可以摆动它,我理想上会像一个永久的象征
facebook.php
<!doctype html>
<html>
<head>
<script type="text/javascript">
function SendToFacebook()
{
window.fbAsyncInit = function () {
// init the FB JS SDK
FB.init({
appId: '432036336937975', // App ID from the app dashboard
status: false, // Check Facebook Login status
xfbml: true // Look for social plugins on the page
});
FB.login(function (response) {
FB.api('/me/accounts', function (apiresponse) {
if (response.authResponse) {
//simple user access token
var accessToken = response.authResponse.accessToken,
ajaxRequest = new XMLHttpRequest(),
pageId = '1521533601413118';
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState === 4) {
//print out the extended page access token
alert(ajaxRequest.responseText);
}
};
ajaxRequest.open('POST','generatePageToken.php?pageId=' + pageId, true);
ajaxRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
ajaxRequest.send('accessToken=' + accessToken);
}
var data = {
message: "mymessage test",
display: 'iframe',
caption: "caption",
name: "name",
description: "description",
to: '1521533601413118',
from: '1521533601413118'
};
FB.api('/1521533601413118/feed', 'post', data, function () {
console.log(arguments);
});
});
}, { scope: 'manage_pages'});
};
// Load the SDK asynchronously
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) { return; }
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));
}
</script>
</head>
<body>
<a href="#" onclick="SendToFacebook();">Submit</a>
</body>
</html>
generatePageToken.php
<?php
$accessToken = $_GET['accessToken'];
$pageId = $_POST['pageId'];
$fbAppId = '432036336937975';
$fbAppSecret = 'REMOVED FOR NOW';
$appsecretProof = hash_hmac('sha256', $accessToken, $fbAppSecret);
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php-3.2');
//get extended user access token
$url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token' .
'&client_id=' . $fbAppId .
'&client_secret=' . $fbAppSecret .
'&fb_exchange_token=' . $accessToken .
'&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
$response_params = array();
parse_str($curlResult, $response_params);
$extendedUserToken = $response_params['access_token'];
$appsecretProof = hash_hmac('sha256', $extendedUserToken, $fbAppSecret);
//get extended page access token
$url = 'https://graph.facebook.com/' . $pageId .
'?fields=access_token' .
'&access_token=' . $extendedUserToken .
'&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
curl_close($ch);
$pageToken = json_decode($curlResult)->access_token;
echo $pageToken;
?>
答案 0 :(得分:0)
我认为这是因为你混淆了$_POST
和$_GET
:
您的代码:
ajaxRequest.open('POST','generatePageToken.php?pageId=' + pageId, true);
^ pageId is GET
ajaxRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
ajaxRequest.send('accessToken=' + accessToken);
^ accessToken is POST
您将pageId
作为GET变量发送,而accessToken
是POST变量。
但是,您的PHP将变量检索为:
$accessToken = $_GET['accessToken'];
$pageId = $_POST['pageId'];
应该是相反的方式,例如:
$accessToken = $_POST['accessToken'];
$pageId = $_GET['pageId'];
如果您想以页面的用户身份在自己的网页上发帖,则需要先更改权限范围:
您需要}, { scope: 'manage_pages, publish_stream'});
,(添加了publish_stream)。
现在,它很容易发布,只需发送一个POST请求:
$data = http_build_query(array(
'access_token' => **your access token**,
'message' => 'hello world!'
));
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v2.1/1521533601413118/feed');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);