Php 必须使用活动访问令牌查询有关当前用户的信息|访问令牌似乎有效
我已经定义了一个访问令牌,并且拥有“喜欢”facebook页面所需的权限,但firebug一直在标题中告诉我错误请不要将此帖子标记为重复的,因为我已经查看了有关此问题的帖子,但我找不到任何适合我具体情况的答案。 我不明白为什么或者如何解决它。我的代码如下: utils.phpPhp 必须使用活动访问令牌查询有关当前用户的信息|访问令牌似乎有效,php,javascript,facebook,facebook-graph-api,permissions,Php,Javascript,Facebook,Facebook Graph Api,Permissions,我已经定义了一个访问令牌,并且拥有“喜欢”facebook页面所需的权限,但firebug一直在标题中告诉我错误请不要将此帖子标记为重复的,因为我已经查看了有关此问题的帖子,但我找不到任何适合我具体情况的答案。 我不明白为什么或者如何解决它。我的代码如下: utils.php <?php require_once('sdk/src/facebook.php'); require_once("AppInfo.php"); /** * @return the value at $in
<?php
require_once('sdk/src/facebook.php');
require_once("AppInfo.php");
/**
* @return the value at $index in $array or $default if $index is not set.
*/
function idx(array $array, $key, $default = null) {
return array_key_exists($key, $array) ? $array[$key] : $default;
}
function he($str) {
return htmlentities($str, ENT_QUOTES, "UTF-8");
}
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'sharedSession' => true,
'trustForwarded' => true,
'file_upload' =>true
));
$user_id = $facebook->getUser();
if($user_id)
{
$logoutUrl =$facebook->getLogoutUrl();
}
else
{
$loginUrl=$facebook->getLoginUrl();
}
if ($user_id) {
try {
// Fetch the viewer's basic information
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
// If the call fails we check if we still have a user. The user will be
// cleared if the error is because of an invalid accesstoken
if (!$facebook->getUser()) {
header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
exit();
}
}
}
?>
li{
垂直对齐:中间对齐;
垫面:1米;
}
(功能(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”+(debug?/debug):“)+”.js”;
ref.parentNode.insertBefore(js,ref);
}(文件,虚假))//加载javascript sdk
$(函数(){
//设置以便我们处理单击按钮
$(“#类似于但”)。单击(函数(){
FB.api(
“me/og.likes”,
"岗位",,
{
对象:“http://facebook.com/216730015024772"
},
功能(响应){
警报(响应);
});});}); // 它们都关好了,不用麻烦检查了。(!)应该喜欢“对象”
有人知道为什么会出现错误,或者我如何解决这个问题吗?任何暗示都将不胜感激
注意:用户从另一个index.php登录,但我不会在这里发布它,因为它没有问题,并且访问令牌仍然收集在utils.php中。另外,在检查“likes.php”中是否授予了权限时,它也可以正常工作。假设您从Facebook收到的
access\u令牌确实有效。。。看起来您的Facebook javascript SDK的实现还没有完成
您已经包含了库加载器,但没有FB.init()代码>节初始化库。在第二个代码块中,我看不到对任何appId
的引用,因此Facebook有办法知道您的代码引用的是什么应用程序
请参阅
具体来说,这可以解决您的问题:
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'YOUR_APP_ID', // App ID from the App Dashboard
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
xfbml : true // parse XFBML tags on this page?
});
// Additional initialization code such as adding Event Listeners goes here
document.getElementById('like_but').addEventListener('click', function() {
FB.api('me/og.likes', 'post', {
object: "http://facebook.com/216730015024772"
}, function(response) {
console.log(response);
});
}, false);
};
(function(d, debug){
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" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));
我将编辑问题,我忘了指定。在另一个.php文件中调用fb.init,并初始化所有内容。“likes.php”是从该页面弹出的,您正在混合javascript和php的角色。不管FB
对象是否在另一个.php文件中初始化。您位于当前的.php文件中,并且浏览器的DOM已重置,这意味着您必须重新初始化FB对象。我对这一点很陌生。好的,但是,如果我在我的utils.php中初始化它,这对两者都有效吗?php SDK的初始化并不意味着JS SDK已经准备好了。在任何需要通过javascript实现Facebook功能的页面上,都需要FB.init
块。将我粘贴的内容合并到likes.php
,只要用户经过身份验证,它就可以工作。php和JS SDK是独立的实体。两者都是将Facebook用户连接到您的应用程序的一种方式,但它们彼此不通信。分别对待。
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'YOUR_APP_ID', // App ID from the App Dashboard
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File for x-domain communication
status : true, // check the login status upon init?
cookie : true, // set sessions cookies to allow your server to access the session?
xfbml : true // parse XFBML tags on this page?
});
// Additional initialization code such as adding Event Listeners goes here
document.getElementById('like_but').addEventListener('click', function() {
FB.api('me/og.likes', 'post', {
object: "http://facebook.com/216730015024772"
}, function(response) {
console.log(response);
});
}, false);
};
(function(d, debug){
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" + (debug ? "/debug" : "") + ".js";
ref.parentNode.insertBefore(js, ref);
}(document, /*debug*/ false));