0

Facebook SDK for PHPは初めてです。ここで非常に基本的な質問があります (この回答は役に立ちませんでした): 現在、Facebook プロファイルにログインしていますが、getUser()何も返されません。なんで?

require_once("vendor/facebook-php-sdk/src/facebook.php");

class Controller_Test extends Controller {
    public function action_me()
        {
            $config = array();
            $config['appId'] = 'dfjq454...';
            $config['secret'] = 'adfw424...';
            $config['fileUpload'] = false;
            $facebook = new Facebook($config);  
            $user = $facebook->getUser();

            // Returns '0'
            echo "Me: ".$user;
            // ...

編集1:

bootstrap.php でこの行のコメントを外してみましたが、うまくいきませんでした'auth' => MODPATH.'auth', // Basic authentication

編集2: 次のコードを実行すると

$user_profile = $facebook->api('/me');
        echo "My Profile: ".$user_profile;

このエラーが発生します

現在のユーザーに関する情報を照会するには、アクティブなアクセス トークンを使用する必要があります。

編集3

FacebookApiException [ 0 ]: An active access token must be used to query information about the current user.
DOCROOT\vendor\facebook-php-sdk\src\base_facebook.php [ 1272 ]
1267    *
1268    * @param $result array A record storing the error message returned
1269    *                      by a failed API call.
1270    */
1271   protected function throwAPIException($result) {
1272     $e = new FacebookApiException($result);
1273     switch ($e->getType()) {
1274       // OAuth 2.0 Draft 00 style
1275       case 'OAuthException':
1276         // OAuth 2.0 Draft 10 style
1277       case 'invalid_token':
DOCROOT\vendor\facebook-php-sdk\src\base_facebook.php [ 881 ] » BaseFacebook->throwAPIException(arguments)
{PHP internal call} » BaseFacebook->_graph(arguments)
DOCROOT\vendor\facebook-php-sdk\src\base_facebook.php [ 654 ] » call_user_func_array(arguments)
APPPATH\classes\Controller\test.php [ 17 ] » BaseFacebook->api(arguments)
SYSPATH\classes\Kohana\Controller.php [ 84 ] » Controller_Test->action_me()
{PHP internal call} » Kohana_Controller->execute()
SYSPATH\classes\Kohana\Request\Client\Internal.php [ 97 ] » ReflectionMethod->invoke(arguments)
SYSPATH\classes\Kohana\Request\Client.php [ 114 ] » Kohana_Request_Client_Internal->execute_request(arguments)
SYSPATH\classes\Kohana\Request.php [ 986 ] » Kohana_Request_Client->execute(arguments)
DOCROOT\index.php [ 118 ] » Kohana_Request->execute()
4

2 に答える 2

2

Facebook 開発者ドキュメントから:

注: ネイティブ/デスクトップ アプリとして構成されているアプリケーションは、アプリケーションの access_token を必要とする API 呼び出しを行うことができません。

[設定] → [詳細設定] に移動し、次のように変更しWebます。

これがこの問題の最も一般的な原因であり、変更するとWeb解決するはずです。


$_REQUESTそうでない場合は、デフォルト設定が変更されたため、PHP 5.4.x で変更されたサーバーからのスーパーグローバル配列に依存する PHP SDK に問題がある可能性がありますphp.ini。回避策は、 with 、およびスーパーグローバル配列で使用されているものを模倣することです。 source $_REQUESTbase_facebook.php$_GET$_POST$_COOKIE

で次のコードを見つけますbase_facebook.php

<?php

protected function getCode() {
    if (isset($_REQUEST['code'])) {
        if ($this->state !== null &&
                isset($_REQUEST['state']) &&
                $this->state === $_REQUEST['state']) {

            // CSRF state has done its job, so clear it
            $this->state = null;
            $this->clearPersistentData('state');
            return $_REQUEST['code'];
        } else {
            self::errorLog('CSRF state token does not match one provided.');
            return false;
        }
    }

    return false;
}

?>

次のように変更します。

<?php

protected function getCode() {
    $server_info = array_merge($_GET, $_POST, $_COOKIE);

    if (isset($server_info['code'])) {
        if ($this->state !== null &&
                isset($server_info['state']) &&
                $this->state === $server_info['state']) {

            // CSRF state has done its job, so clear it
            $this->state = null;
            $this->clearPersistentData('state');
            return $server_info['code'];
        } else {
            self::errorLog('CSRF state token does not match one provided.');
            return false;
        }
    }

    return false;
}

?>

違いを見ます。

于 2013-09-11T16:28:38.147 に答える
0

私は同様の問題に悩まされていました。これがあなたを助けるかもしれない解決策です.

 $facebook = new Facebook(array(
    'appId'  => 'xxxx',
    'secret' => 'xxxxx',
    "redirect_uri" => "set the uri to point to the same page"
 ));

$user = $facebook->getUser();

if($user)
{
//your task
} else {
header('location:'. $facebook->getLoginUrl());

}

$user = $facebook->getUser();

if($user)
{
 //your task
} else {
header('location:'. $facebook->getLoginUrl());

}
于 2014-02-13T06:53:50.253 に答える