LoginSignup
6
7

More than 5 years have passed since last update.

FuelPHP 1.7.3 でTwitterのOpauth ログインする方法

Posted at

GitHubのfuel-opauth (https://github.com/andreoav/fuel-opauth )には、

Repository outdated and no longer supported.

と記載されているので、使わない方向で実装する方法。


基本的には上記URLのドキュメントに従えばOK.


あらかじめ Twitter Appsの登録をしておく。その際、ダミーURLで良いのでCallback URLを指定しておく。 https://apps.twitter.com/

opauthパッケージとTwitterストラテジーをインストール

compose.json に opauth/opauth, opauth/twitter を追加して、 php composer.phar updateを実行する

    "require": {
        "php": ">=5.3.3",
        "composer/installers": "~1.0",
        "fuel/docs": "dev-1.7/master",
        "fuel/core": "dev-1.7/master",
        "fuel/auth": "dev-1.7/master",
        "fuel/email": "dev-1.7/master",
        "fuel/oil": "dev-1.7/master",
        "fuel/orm": "dev-1.7/master",
        "fuel/parser": "dev-1.7/master",
        "fuelphp/upload": "2.0.2",
        "monolog/monolog": "1.5.*",
        "michelf/php-markdown": "1.4.0",
        "opauth/opauth" : "0.4.*",
        "opauth/twitter" : "dev-master"
    },

※ opauthのページのChangeLog を見ると、v0.4.4 (10 May 2013) が最新なので、0.4.*を指定している。

opauthの設定を記述

デフォルトの設定ファイル fuel/vendor/opauth/opauth/example/opauth.conf.php.defaultfuel/app/config/opauth.php にコピーする。

fuel/app/config/opauth.php の、
- path
- callback
- security salt
- Starategy
を以下のように指定する。

return array(

   'path' => '/url/path/to/controller/action/',
   'callback_url' => '/url/path/to/controller/callback/',
   'security_salt' => "random string",   
   'Strategy' => array(
   'Twitter' => array(
        'key' => 'Consumer Key',
        'secret' => 'Consumer Secret',
    ),
);

※ それぞれ簡単な説明があるので、それに従う

コントローラーを作成

Twitterの認証ページにリダイレクトするURLは /path/to/controller/action/twitter/ になる。
※ /twitter/ は必須。

<?php

 class Controller_Login extends Controller
 {

    public function action_action( $provider = null ){
         \Auth_Opauth::forge();
     }

    public function action_callback(){
         $_opauth = \Auth_Opauth::forge( false )
         // $_opauth の値を使ってゴニョゴニョする
     }

 }
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7