redirect_uri URL 必须是绝对的 Facebook 登录 PHP SDK

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16992958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:01:36  来源:igfitidea点击:

The redirect_uri URL must be absolute Facebook login PHP SDK

phpfacebookcodeignitersdk

提问by user2232273

I am trying to implement the facebook login in my project in codeigniter.

我正在尝试在我的项目中的 codeigniter 中实现 facebook 登录。

I am using the php sdk.

我正在使用 php sdk。

When I click on the login button, it gives me an error:

当我点击登录按钮时,它给了我一个错误:

The redirect_uri URL must be absolute

What is the problem and how do I fix it?

有什么问题,我该如何解决?

Here is the contents of view/home.php:

以下是内容view/home.php

 <?php if(!$fb_data['me']): ?>
  Please login with your FB account: 
  <a href="<?php echo $fb_data['loginUrl']; ?>">login</a>
  <?php else: ?>
  <img src="https://graph.facebook.com/<?php 
    echo $fb_data['uid']; ?>/picture" alt="" class="pic" />
  <p>Hi <?php echo $fb_data['me']['name']; ?>,<br />
    <a href="<?php 
    echo site_url('welcome/topsecret'); ?>">
    You can access the top secret page</a> or 
    <a href="<?php echo $fb_data['logoutUrl']; ?>">logout</a> </p>
  <?php endif; ?>

Here is the contents of: controler/home.php

以下是内容: controler/home.php

class Home extends CI_Controller {

   function __construct()
    {
        parent::__construct();

        $this->load->model('Facebook_model');
    }

    function index(){
        $this->load->model("Concertos_model");
        $data['banners'] = $this->Concertos_model->get_banner_data();


       $fb_data = $this->session->userdata('fb_data'); // This array contains all the user FB information

        $data = array(
                    'fb_data' => $fb_data,
                    );

        $this->load->view('home', $data);   

    }

    function topsecret()
    {
        $fb_data = $this->session->userdata('fb_data');

        if((!$fb_data['uid']) or (!$fb_data['me']))
        {
            redirect('home');
        }
        else
        {
            $data = array(
                        'fb_data' => $fb_data,
                        );

            $this->load->view('home', $data);
        }
    }

}

Contents of: model/Facebook_model.php:

内容:model/Facebook_model.php

<?php
class Facebook_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();

        $config = array(
                        'appId'  => 'xxxxxxxxxxxxxxxxxxx',
                        'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxx',
                        'fileUpload' => true, // Indicates if the CURL based @ syntax for file uploads is enabled.
                        );

        $this->load->library('Facebook', $config);

        $user = $this->facebook->getUser();

        // We may or may not have this data based on whether the user is logged in.
        //
        // If we have a $user id here, it means we know the user is logged into
        // Facebook, but we don't know if the access token is valid. An access
        // token is invalid if the user logged out of Facebook.
        $profile = null;
        if($user)
        {
            try {
                // Proceed knowing you have a logged in user who's authenticated.
                $profile = $this->facebook->api('/me?fields=id,name,link,email');
            } catch (FacebookApiException $e) {
                error_log($e);
                $user = null;
            }
        }

        $fb_data = array(
                        'me' => $profile,
                        'uid' => $user,
                        'loginUrl' => $this->facebook->getLoginUrl(
                            array(
                                'scope' => 'email,user_birthday,publish_stream', // app permissions
                                'redirect_uri' => 'home' // URL where you want to redirect your users after a successful login
                            )
                        ),
                        'logoutUrl' => $this->facebook->getLogoutUrl(),
                    );

        $this->session->set_userdata('fb_data', $fb_data);
    }
}

Why does facebook return that message and how do I fix it?

为什么 facebook 会返回该消息,我该如何解决?

回答by dev-null-dweller

'redirect_uri' => 'home' // URL where you want to redirect your users after a successful login

You have to provide full URL (starting from http://), not CI page alias. Use site_url('home')instead

您必须提供完整的 URL(从 开始http://),而不是 CI 页面别名。使用site_url('home')替代