سؤال

مرحبًا، أود أن أتعلم شيئًا ما من هنا، أريد بشكل أساسي أن يكون الوصول إلى نظامي في حساب Google بطريقة ما، وأتمكن من القيام بما يلي

  • الحصول على معرف العميل وإعادة توجيه Uris والمفاتيح السرية
  • المصادقة من حساب جوجل
  • الحصول على رمز

لكنني شعرت أنني كنت أرتكب خطأً في جزء ما، هذا هو Oauth2callback

class Oauth2callback extends CI_Controller {


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

        $this->load->helper('url');
        $this->load->library('session');
        require_once APPPATH.'libraries/Google/Client.php';
        session_start();


    }

    public function index()
    {


        $client_id = $this->config->item('client_id');
        $client_secret = $this->config->item('client_secret');
        $redirect_uri = $this->config->item('redirect_uri');

        $client = new Google_Client();
        $client->setClientId($client_id);
        $client->setClientSecret($client_secret);
        $client->setRedirectUri($redirect_uri);
        $client->addScope("https://www.googleapis.com/auth/userinfo.email");
        $client->addScope("https://www.googleapis.com/auth/userinfo.profile");

        if (isset($_GET['code'])) {
            $client->authenticate($_GET['code']);
            $_SESSION['access_token'] = $client->getAccessToken();
            $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

            header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
        }

        if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
            $client->setAccessToken($_SESSION['access_token']);
        } else {
            $authUrl = $client->createAuthUrl();
        }

        if ($client->getAccessToken()) {
            $_SESSION['access_token'] = $client->getAccessToken();


        }


        if(isset($authUrl)) {
            header('location:'. base_url());
        }else{
            header('location:'. base_url().'dashboard');
        }
    }

ولكن هذه هي وحدة تحكم الفهرس الخاصة بي Login لديها زر فقط sign in with Google

class Login extends CI_Controller {


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

        $this->load->helper('url');
        $this->load->library('session');
        require_once APPPATH.'libraries/Google/Client.php';
        session_start();
    }

    public function index()
    {
        //$this->checkSession();
        $client_id = $this->config->item('client_id');
        $client_secret = $this->config->item('client_secret');
        $redirect_uri = $this->config->item('redirect_uri');

        $client = new Google_Client();
        $client->setClientId($client_id);
        $client->setClientSecret($client_secret);
        $client->setRedirectUri($redirect_uri);
        $client->addScope("https://www.googleapis.com/auth/userinfo.email");
        $client->addScope("https://www.googleapis.com/auth/userinfo.profile");

            $authUrl = $this->data['authUrl'] = $client->createAuthUrl();
            $this->load->view('login/index.php',$this->data);
            $this->load->view('template/pre_footer');
            $this->load->view('template/footer');


    }
}


ما هي العملية الصحيحة لهذا باستخدام MVC PHP الذي أحتاجه للقيام بما يلي.:
أ.اضغط على الزر sign in to google
ب.احصل على الرمز المميز واحفظه في الجلسة
ج.استخدم الرمز المميز لنظامي بأكمله (في كل وحدة تحكم)

ما لدي الآن هو A & B ولكن C لا أعرف تمامًا ما يجب فعله.

يمكن لأي شخص أن يساعدني في هذا.أي تعليق اقتراح هو موضع تقدير جيد.شكرا لك مقدما.

هل كانت مفيدة؟

المحلول

قد يكون الرمز الذي لديك مربكًا بعض الشيء لأنه يتعامل مع عنوان URL للتفويض، وإعادة التوجيه إلى Google، ورد الاتصال بموقعك، وحفظ رمز الوصول في نفس الوقت.

عندما يتعلق الأمر باستخدام رمز الوصول لإجراء مكالمة مصادق عليها إلى واجهة برمجة التطبيقات، فالأمر أبسط بكثير.إذا كان لديك رمز وصول صالح مخزّن في جلستك، فيمكنك استخدامه في أي مكان لتهيئة تطبيق Google، بهذه الطريقة:

// client
$client = new Google_Client();
$client->setApplicationName('Google Application');
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);

// token
$client->setAccessToken($token);

الآن، إذا كنت تريد أن يكون هذا متاحًا عبر العديد من وحدات التحكم، فإن الطريقة الأكثر ملاءمة في CodeIgniter هي إنشاء مكتبة جديدة تحتوي على الكود أعلاه.

تتمثل ميزة استخدام المكتبة في أنه يمكن تحميلها تلقائيًا في تكوين CodeIgniter، ويمكن الوصول إليها بسهولة في أي مكان في التعليمات البرمجية الخاصة بك.

مكتبة المثال:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Someclass {

    private $client;

    public function __construct()
    {
        ... 

        // client
        $client = new Google_Client();
        $this->client->setApplicationName('Google Application');
        $this->client->setClientId($clientId);
        $this->client->setClientSecret($clientSecret);
        $this->client->setRedirectUri($redirectUri);

        // token
        $this->client->setAccessToken($token);
    }
}

?>

ثم عليك فقط القيام بذلك لاستخدامه في وحدة التحكم الخاصة بك:

 $this->load->library('someclass');

يمكنك أيضًا إنشاء اختصارات لواجهات برمجة تطبيقات محددة.على سبيل المثال، إذا كنت تريد الوصول السريع إلى Google Analytics API، فيمكنك القيام بذلك:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Someclass {

    private $client;

    public function __construct()
    {
        ... 

        // client
        $client = new Google_Client();
        $this->client->setApplicationName('Google Application');
        $this->client->setClientId($clientId);
        $this->client->setClientSecret($clientSecret);
        $this->client->setRedirectUri($redirectUri);

        // token
        $this->client->setAccessToken($token);
    }

    public function analytics()
    {
        return new Google_Service_Analytics($this->client);
    }
}

?>

ثم استخدمه بهذه الطريقة في وحدة التحكم الخاصة بك:

 $this->load->library('someclass');
 $this->someclass->analytics();

تعرف على المزيد حول مكتبات CodeIgniter:

http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top