我在我的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