質問

こんにちは、私はここから何かを学びたい、基本的に私は私のシステムのアクセスがGoogleアカウントに入ることをどういうわけか私は次の

を実行することを管理します

  • クライアントIDを取得し、URIのリダイレクト、秘密鍵
  • Googleアカウントからの認証
  • トークン
  • を取得します

しかし私は私が何らかの一部で間違っていると感じました、これは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を使用して、FFを実行する必要があるという正しいプロセスとなるでしょう。:
A. [sign in to google]ボタンをクリックします B.トークンを入手してセッションに保存する
C.システム全体にトークンを使用しました(すべてのコントローラ内)

私が今持っているものはA&Bですが、C私は何をするのかわからない。

誰もがこれを手伝ってくれることができます。提案コメントはよく評価されています。事前にありがとうございます。

役に立ちましたか?

解決

認証URLを処理し、Googleへのリダイレクト、サイトへのコールバック、およびアクセストークンを同時に保存し、アクセストークンを保存し、アクセストークンを保存してください。

アクセストークンを使用してAPIを認証した呼び出しを実行するようになると、はるかに簡単です。セッションに保存されている有効なアクセストークンがある場合は、本当に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 Configで自動的にロードでき、コード内のどこにでも簡単にアクセスできます。

例ライブラリ:

<?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');
.

特定のAPIへのショートカットを作成することもできます。たとえば、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/ create_libraries.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top