2013-05-01 28 views
0

我真的需要在這個一個有些幫助..Facebook的PHP-SDK沒能獲得用戶ID(我得到0)無論什麼

我嘗試喜歡在這裏Facebook PHP SDK $facebook->getSignedRequest() Get User ID and Name. (Page Tab)創建與鏈接的應用程序facebook page..so far so good .. 問題是我無法檢索用戶ID,無論如何,使用php-sdk ..

我也試過這個修復,https://www.webniraj.com/2012/12/19/facebook-php-sdk-fixing-getuser-on-php-5-4-x/但它沒有做任何區別

require 'php-sdk/facebook.php'; 
// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => 'myappid', 
    'secret' => 'mysecretid', 
)); 

$theRequest = $facebook->getSignedRequest(); 
$user = $facebook->getUser(); 
var_dump($user); 

無論我嘗試過什麼我一直沒能檢索用戶ID ..

我想保羅建議

用下面的代碼

if ($_REQUEST) { 
    $signed_request = $_REQUEST['signed_request']; 
} else { 
    echo '$_REQUEST is empty'; 
} 

function parse_signed_request($signed_request) { 
    list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

    // decode the data 
    $sig = base64_url_decode($encoded_sig); 
    $data = json_decode(base64_url_decode($payload), true); 

    return $data; 
} 

function base64_url_decode($input) { 
    return base64_decode(strtr($input, '-_', '+/')); 
} 

$data = parse_signed_request($signed_request); 
var_dump($data); 

我得到一個「NULL」作爲響應..鏈接:S

秒編輯..我得到這個代碼

$ information = parse_signed_request($ signed_request,$ secret); $ oauth_token = $ information [「oauth_token」];

var_dump($ information); var_dump($ oauth_token);

$app_id = "myappid"; 
$canvas_page = "https://apps.facebook.com/app_namespace"; 
$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" 
     . $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=read_stream, friends_likes"; 

$signed_request = $_REQUEST["signed_request"]; 

list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); 

if (empty($oauth_token)) {echo("<script> top.location.href='" . $auth_url . "'</script>");} 
這樣

,我設法得到身份驗證對話框,但在重定向,我得到錯誤配置app..if的消息,我再次進入應用程序,然後我訪問所有必要info..token等。 所以如果我找到一個解決方案爲什麼在重定向我得到錯誤配置的應用程序,我想我有點解決了我的問題?

+0

沒有用戶授權您的應用程序的代碼? – 2013-05-01 07:23:38

+0

我現在正在使用它作爲開發人員,所以我想我默認授權? – nikolas 2013-05-01 07:27:09

+0

我不知道你是否閱讀過這個:https://developers.facebook.com/docs/howtos/login/signed-request/ 但是,對於一個頁面應用程序,你必須:在你的頁面上註冊應用程序,只嘗試在頁面加載時解碼signed_request(基本上,點擊頁面上的圖標以訪問您的頁面應用程序後) – PoulsQ 2013-05-01 07:32:01

回答

0

首先,signed_request不給user_idoauth_token之前,除非用戶已經登錄到您的app.It只會給有關user關鍵這是一個包含語言環境字符串JSON對象中的用戶的基本信息,國家字符串和年齡對象。
所以,如果你想獲得user_id實施Login或測試用例在這裏,你可以做以下

<?php 
$params = array(
    'redirect_uri' => 'http://yourdomain.com/post_login_page' 
); 

$loginUrl = $facebook->getLoginUrl($params); 
if(!$facebook->getUser()): 
?> 
<script>window.location.href = "<?php $loginUrl; ?>"</script> 
<?php endif ?> 
+0

我使用 $ params = array( 'scope'=>'read_stream,friends_likes', );並且我沒有認證沒有任何東西...我不知道發生了什麼事情.. $ loginUrl = $ facebook-> getLoginUrl($ params); var_dump($ loginUrl); – nikolas 2013-05-01 08:12:18

+0

@nikolas你需要重定向到登錄URL才能進行身份驗證 – 2013-05-01 08:17:48

+0

我設法得到簽名請求,但沒有成功登錄..和用戶ID ..:S – nikolas 2013-05-01 08:40:25

1

我不知道誰可以找到有用的這個或不.. 我認爲這將是一個很好的想法,如果Facebook的,建議離散的情況下,每個sdk更好適用.. 花費了幾個小時後,在頁面選項卡上集成facebook應用程序的php sdk,所以,Facebook上的應用程序是沒有問題的,我結束了javascritp sdk將更好地服務我的目的,因爲, a)它不重定向用戶,從我的頁面到驗證 b)它不需要應用程序作爲一個Facebook應用程序也是存在的(在我的情況下,我希望顯示爲一個頁面標籤) c)它使所有內容重疊,提出auth對話框,然後返回到我的頁面, roviding所有有用的信息..

複製從幾個FB來源這個工作對我來說

FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
    // the user is logged in and has authenticated your 
    // app, and response.authResponse supplies 
    // the user's ID, a valid access token, a signed 
    // request, and the time the access token 
    // and signed request each expire 
    uid = response.authResponse.userID; 
    //alert(uid); 
    var accessToken = response.authResponse.accessToken; 
    } else if (response.status === 'not_authorized') { 
    // the user is logged in to Facebook, 
    // but has not authenticated your app 
      FB.login(function(response) { 
       if (response.authResponse) { 
       uid = response.authResponse.userID; 
       console.log('Welcome! Fetching your information.... '); 
       FB.api('/me', function(response) { 
        }); 
      } else { 
      } 
      }); 
     } else { 
    // the user isn't logged in to Facebook. 
     } 
}); 

// Additional initialization code such as adding Event Listeners goes here 

    }; 
相關問題