我在我的index.php下面的代码:Facebook应用程序重定向
$config = array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'cookie' => true
);
$facebook = new Facebook($config);
$app_name = "AppName";
$fbPermissions = 'email';
$validUser = 1;
$fbuser = $facebook->getUser(); // get user
$fbname = "";
$fbmail = "";
$fbid = 0;
$user_profile = null;
if(!$fbuser)
{
$validUser = 0;
}
else
{
try {
$user_profile = $facebook->api('/me'); //user profile
$user_permissions = $facebook->api("/me/permissions"); //list of user permissions
} catch (FacebookApiException $e) {
$fbuser = null;
$user_permissions = null;
}
$permissions_needed = explode(',',$fbPermissions); // permission required to proceed
foreach($permissions_needed as $per) //loop thrugh each permission
{
if ($user_permissions != null) {
if(!array_key_exists($per, $user_permissions['data'][0])){
$validUser = 0;
}
}
}
}
if($validUser == 1){
$fbname = $user_profile["last_name"]." ".$user_profile["first_name"];
$fbmail = $user_profile["email"];
$fbid = $user_profile["id"];
}
if(isset($_POST['submit'])) {
header('Location: writetocsv.php?id='.$_POST['fbid'].'&name='.$_POST['fbname'].'&email='.$_POST['email']) ;
}
if($signed_request = parsePageSignedRequest()){
if($signed_request->page->liked) {
if(!userIsInDatabase($fbid)){
include("pages/fanpage.php");
echo "fanpage";
}
else{
include("pages/thankyou.php");
echo "thankyoupage";
}
} else {
include("pages/notfanpage.php");
echo "notafanpage";
}
}
?>
<script>
// Additional JS functions here
function login() {
FB.login(function(response) {
if (response.authResponse) {
// connected
} else {
// cancelled
}
},{scope:"email"});
}
window.fbAsyncInit = function() {
FB.init({
appId : <?php echo AppInfo::appID(); ?> , // App ID
channelUrl : 'channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional init code here
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
} else if (response.status === 'not_authorized') {
// not_authorized
login();
} else {
// not_logged_in
login();
}
});
};
// 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));
</script>
好了,让我来解释一下。当新用户连接到我的标签页应用程序页面时,会显示他notafanpage.php。当他喜欢我的页面后,该标签会自动刷新并显示他fanpage.php。在这个fanpage.php我有一个名称和邮件,我需要收集的形式。 按他提交后,它重定向到index.php它验证输入(尚未完成),然后返回到writetocsv.php?id = .. & name = .. & mail = ..我在哪里写数据到一个csv,然后重定向到index.php。这是它被窃听的地方。它说OAuthException:必须使用活动的访问令牌来查询有关当前用户的信息。有什么建议么 ?
编辑:
嗯,我想出了自己。对于有此问题的人来说,解决方案如下:
Facebook仅在您第一次访问该应用程序时发送signedrequest。所以你必须将签名的请求存储在一个会话中。至于访问令牌,您必须记住第一个访问令牌,并在页面通过$ facebook-> setAccesToken($ accestoken)方法重新加载时进行设置。祝你好运 !
发布答案不编辑帖子,但在这里创建一个新的答案帖子。然后标记它是正确的。 – 2013-03-17 23:18:51
好吧,我会这样做的!尽管我明天可以回答,因为我刚刚创建了我的帐户,但我有一些限制。 – bandrei2408 2013-03-18 02:16:26