我正在按照本教程(https://developers.facebook.com/docs/mediasolutions/topic_search/v2.9)使用php,ajax和facebook graph api查找搜索查询的相关主题。虽然我为每次登录尝试生成应用程序访问令牌,但我一直收到此错误:“(#15)必须使用app access_token调用此方法。”
我的代码如下:
的的index.php
<html>
<head>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script src="js/jquery.js"></script>
</head>
<body>
<fb:login-button autologoutlink="true" onlogin="OnRequestPermission();">
</fb:login-button>
<script language="javascript" type="text/javascript">
FB.init({
appId: '1619055624801235',
status: true,
cookie: true,
xfbml: true,
version : 'v2.2'
});
</script>
<!-- <button name="my_full_name" onclick="ShowMyName()" value="My Name" /> -->
<script language="javascript" type="text/javascript">
// function ShowMyName() {
// FB.api("/me",
// function (response) {
// alert('Name is ' + response.name);
// });
// }
</script>
<input type = "text" id = "searchQuery" name="searchQuery" placeholder = "Search for posts"></input>
<button name="showPosts" onclick="ShowPosts()" >Show Posts</button>
<script language="javascript" type="text/javascript">
function ShowPosts(){
var query = document.getElementById("searchQuery").value;
var accessToken;
//alert(query);
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
$.ajax({url:"js-login.php",
type: "get",
success:function(response){
accessToken = response;
fbDataCollect(accessToken);
}});
}
});
}
function fbDataCollect(accessToken){
FB.api(
"/search",
{
"type": "topic",
"q": searchQuery,
"fields": "id,name,page",
"access_token": accessToken
},
function (response) {
console.log(accessToken);
console.log(response);
if (response && !response.error) {
/* handle the result */
alert(response);
}
}
);
}
</script>
<div id="fb-root"></div>
</body>
</html>
JS-login.php中
<?php
include 'php-graph-sdk-5.0.0/src/Facebook//autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '1619055624801235',
'app_secret' => '6a4fed7bfe0321bb21ad68afcbe2f640',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getJavaScriptHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
echo 'No cookie set or no OAuth data could be obtained from cookie.';
exit;
}
// Logged in
//echo '<h3>Access Token</h3>';
//var_dump($accessToken->getValue());
$_SESSION['fb_access_token'] = (string) $accessToken;
echo $_SESSION['fb_access_token'];
?>
请帮忙。提前谢谢。