Facebook 必须使用活动访问令牌
我正在尝试让Facebook用户(当前用户)使用以下代码:Facebook 必须使用活动访问令牌,facebook,sdk,token,Facebook,Sdk,Token,我正在尝试让Facebook用户(当前用户)使用以下代码: $app_id = "xxx"; $app_secret = "yyy"; //** Get user information //Create our application instance. $facebook = new Facebook(array( 'appId' => $app_id, 'secret' => $app_secret, )); // Get User ID $user = $f
$app_id = "xxx";
$app_secret = "yyy";
//** Get user information
//Create our application instance.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');....
// The $user_profile = $facebook->api('/me'); line throw an exception:
'message' => string 'An active access token must be used to query
'information about the current user.' (length=80)
'type' => string 'OAuthException' (length=14)
'code' => int 2500
为什么?您似乎没有按照中所述完成2.0身份验证/授权过程 这里有一些例子解释如何做到这一点。我也在使用PHPSDK,但我选择在客户端通过JavaScript进行身份验证,这对我来说比较容易。但是,这两种方法在文档中都有说明 更新:我使用的代码是PHP和JavaScript的组合,对我来说非常有用。这里唯一没有正确处理的事情(AFAIK)是当用户在未登录Facebook的情况下访问应用程序时,也就是说,他直接通过URL而不是Facebook访问应用程序。在这种情况下,将显示一个空白页面,而不是通知和登录按钮之类的内容 无论如何,这是我的
index.php
,其中我将config.inc.php
中的变量传递给JavaScript,例如成功页面(应用程序主页)和失败页面(用户未授予perms):
<?php
require 'include/config.inc.php';
//Check whether Facebook OAuth mechanism called back to this script with access_token or error
if (isset($_GET['expires_in']) && $_GET['expires_in']>0)
{
header('Location: '.$appname_canvasPage.$appname_successPage);
exit;
}
else if (isset($_GET['error']))
{
//echo 'querystr: '.$_SERVER['QUERY_STRING'];
header('Location: '.$appname_canvasPage.$appname_failurePage);
exit;
}
else
{
require 'include/header_metadata.inc.html';
?>
</head>
<body>
<div id="fb-root"></div>
<script>
var appname_canvasURI = '<?php echo $appname_canvasURI; ?>';
var appname_canvasPage = '<?php echo $appname_canvasPage; ?>';
var appname_successPage = '<?php echo $appname_successPage; ?>';
var appname_failurePage = '<?php echo $appname_failurePage; ?>';
var appname_fbPerms = '<?php echo $appname_fbPerms; ?>';
var appname_appid= '<?php echo $appname_appid; ?>';
window.fbAsyncInit = function()
{
FB.init({
appId : appname_appid, // App ID
channelUrl : appname_canvasPage+'/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
// Additional initialization code here
FB.getLoginStatus(function(response)
{
//console.log('getLoginStatus response: ',response);
if (response.authResponse)
{
//user is already logged in and connected
facebookCheckPerms(); // ensure all requires perms are available and if not request them
}
else
{
//app is not authorized or user is logged out
facebookOAuthRedirect();
}
});
};
// Load the SDK Asynchronously
(function()
{
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
//e.src = "http://static.ak.fbcdn.net/connect/en_US/core.debug.js";
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
function facebookCheckPerms()
{
var hasReqPerms=true;
FB.api(
{
method: 'fql.query',
query: 'SELECT '+appname_fbPerms+' FROM permissions WHERE uid=me()'
},
function(response)
{
for(var key in response[0])
{
if(response[0][key]==0)
{
hasReqPerms=false;
}
}
if (hasReqPerms==false)
{
// user does not have required permissions, do OAuth 2.0 redirect to get permissions
facebookOAuthRedirect();
}
else
{
// user has required permissions, start the app.
//console.log('checkperms: user has required permissions, start the app');
top.location.href = appname_canvasPage+appname_successPage;
}
});
}
function facebookOAuthRedirect()
{
var redirectURL = 'https://www.facebook.com/dialog/oauth/?client_id='+appname_appid+'&scope='+appname_fbPerms+'&redirect_uri='+encodeURIComponent(appname_canvasURI)+'&response_type=token';
//console.log('redirectURL: '+redirectURL);
top.location.href = redirectURL;
}
</script>
<?php
}
?>
</body>
</html>
var appname_canvasURI='';
var appname_canvasPage='';
var appname_successPage='';
var appname_failurePage='';
var appname_fbPerms='';
var appname_appid='';
window.fbAsyninit=函数()
{
FB.init({
appId:appname\u appId,//appId
channelUrl:appname\u canvasPage+/channel.html',//频道文件
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
oauth:true,//启用oauth 2.0
xfbml:true//解析xfbml
});
//这里有额外的初始化代码
FB.getLoginStatus(函数(响应)
{
//log('getLoginStatus response:',response);
if(response.authResponse)
{
//用户已登录并连接
facebookCheckPerms();//确保所有需要的perms都可用,如果没有,则请求它们
}
其他的
{
//应用未经授权或用户已注销
facebookOAuthRedirect();
}
});
};
//异步加载SDK
(功能()
{
var e=document.createElement('script');
e、 类型='text/javascript';
e、 src=document.location.protocol+'//connect.facebook.net/en_US/all.js';
//e、 src=”http://static.ak.fbcdn.net/connect/en_US/core.debug.js";
e、 异步=真;
document.getElementById('fb-root').appendChild(e);
}());
函数facebookCheckPerms()
{
var hasReqPerms=true;
FB.api(
{
方法:“fql.query”,
查询:“从uid=me()的权限中选择“+appname\u fbPerms+”
},
功能(响应)
{
for(响应[0]中的var键)
{
如果(响应[0][key]==0)
{
hasReqPerms=false;
}
}
if(hasReqPerms==false)
{
//用户没有所需的权限,请执行OAuth 2.0重定向以获取权限
facebookOAuthRedirect();
}
其他的
{
//用户具有所需权限,请启动应用程序。
//log('checkperms:用户具有所需权限,启动应用程序');
top.location.href=appname\u canvasPage+appname\u successPage;
}
});
}
函数facebookOAuthRedirect()
{
var重定向URL=https://www.facebook.com/dialog/oauth/?client_id=“+appname\u appid+”&范围=“+appname\u fbPerms+”&重定向\u uri=“+encodeURIComponent(appname\u canvasURI)+”&响应\u type=token';
//log('redirectURL:'+redirectURL);
top.location.href=重定向URL;
}
我的代码中是否包含任何示例?但是$user=$facebook->getUser();不为空?