2013-03-07 131 views
2

我已經定義了一個訪問令牌,並具有「喜歡」Facebook頁面所需的權限,但螢火蟲不斷給我錯誤的標題。 請不要將此帖標記爲重複,因爲我查看了關於該問題的帖子,並且找不到適合我的具體情況的答案。 我無法理解爲什麼或者應該如何解決它。我的代碼如下:必須使用活動訪問令牌來查詢有關當前用戶的信息|訪問令牌似乎是有效的

utils.php中

<?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(); 
    } 
} 
} 
?> 

likes.php

 <?php 

require_once("sdk/src/facebook.php"); 
require_once("utils.php"); 
require_once("AppInfo.php"); 
$permissions = $facebook->api('/me/permissions '); 
if(array_key_exists('publish_actions', $permissions['data'][0])) { 
    // Permission is granted! 
    // Do the related task 
    //$post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!')); 
} else { 
    // We don't have the permission 
    // Alert the user or ask for the permission! 
    header("Location: " . $facebook->getLoginUrl(array("scope" => "publish_actions"))); 
} 
?> 
    <!DOCTYPE html> 
    <html xmlns:fb="http://ogp.me/ns/fb#"> 
    <head> 

     <style type="text/css"> 
     li{ 
     vertical-align: middle; 
     padding-top: 1em; 
     } 
    </style> 
    <div id="fb-root"></div> 
     <script type="text/javascript" src="/javascript/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript"> 

     (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, false)); //loads javascript-sdk 

     $(function() { 
      // Set up so we handle click on the buttons 
      $('#like_but').click(function(){ 
      FB.api(
     'me/og.likes', 
     'post', 
     { 
     object: "http://facebook.com/216730015024772" 
     }, 
     function(response) { 
     alert(response); 
     });});}); // they are closed properly, don't bother checking it. (!) Should like the 'object' 
    </script> 
    </head> 
    <body> 
    <div style="position:fixed; display:block"> 
     <input type="button" value="Like" id="like_but"/> 
</div>  
</body> 
</html> 

有沒有人有任何想法,爲什麼錯誤出現,或者我怎麼能解決這個問題?任何暗示將不勝感激。

注意:用戶從另一個index.php登錄,但我不會在這裏發佈它,因爲它沒有問題,訪問令牌仍然收集在utils.php中。另外,當檢查權限是否在「likes.php」中被授予時,它工作正常。

回答

8

假設您從Facebook收到的access_token確實是有效的......它看起來像您的JavaScript JavaScript SDK的實施不完整。

您已經包含了庫加載器,但是您沒有FB.init();部分來初始化庫。我在第二個代碼塊中看不到任何appId的引用,所以Facebook有辦法知道您的代碼參考的是什麼應用程序。請參閱following documentation from 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)); 
+0

我會編輯的問題,我忘了指定。 fb.init在另一個.php文件中調用,並且所有內容都被初始化。 「likes.php」是從該頁面彈出的 – 2013-03-17 20:40:57

+0

您正在混合javascript和php的角色。 FB'對象在* another * .php文件中被初始化並不重要。您處於當前的.php文件中,並且瀏覽器的DOM已重置,這意味着您必須重新初始化FB對象。 – 2013-03-17 20:42:08

+0

我對此很新穎。好的,但是,如果我在我的utils.php中初始化它,這對兩者都有效嗎? – 2013-03-17 20:44:19

相關問題