facebook php canvas app oauth对话框页面错误

时间:2014-01-09 04:43:57

标签: php facebook cakephp facebook-php-sdk facebook-canvas

我正在尝试创建一个Facebook画布应用程序。我正在使用facebook php-sdk和cakephp。

这是我的登录功能: -

public function login() {

       $app_id = "xxxxxxxxxxx";
       $app_secret = "xxxxxxxxxxxxxxxxxx";
       $canvas_page = "https://apps.facebook.com/xxxxxxx";
       $scope = 'email,publish_actions';
       $facebook = new Facebook(array(
                                'appId'  => $app_id,
                                'secret' => $app_secret
                                ));

       $user = $facebook->getUser();

       if ($user) {

                try {

                // Proceed knowing you have a logged in user who's authenticated.
                $user_profile = $facebook->api('/me');
                $access_token = $facebook->getAccessToken();
                $fbid = $user_profile['id'];
                pr($user_profile);

                } catch (FacebookApiException $e) {

                error_log($e);
                $user = null;

                }

        } else {

                $loginUrl = $facebook->getLoginUrl(array(
                                                   'scope' => $scope,
                                                   'redirect_uri' => $canvas_page
                                                   ));
                print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
        }

}

如果用户已经过身份验证,这似乎有效。但对于新用户而言,它不会显示oauth对话框,而是抛出此错误: -

"Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains."

我在localhost上测试它。所以我的画布网址为http://localhost/xxxxx/     有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案: -

两个步骤: -

a)将redirect_uri更改为http://localhost/xxxxxx

b)添加了一个检查,以查看para params是否有代码并将标题设置为画布页面。

正确代码: -

public function login(){

   $app_id = "xxxxxxxxxxx";
   $app_secret = "xxxxxxxxxxxxxxxxxx";
   $canvas_page = "https://apps.facebook.com/xxxxxxx";
   $scope = 'email,publish_actions';
   $facebook = new Facebook(array(
                            'appId'  => $app_id,
                            'secret' => $app_secret
                            ));
   if (isset($_GET['code'])) {
      header("Location: " . $canvas_page);
      exit;
   }

   $user = $facebook->getUser();

   if ($user) {

            try {

            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
            $access_token = $facebook->getAccessToken();
            $fbid = $user_profile['id'];
            pr($user_profile);

            } catch (FacebookApiException $e) {

            error_log($e);
            $user = null;

            }

    } else {

            $loginUrl = $facebook->getLoginUrl(array(
                                               'scope' => $scope,
                                               ));
            print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
    }

}