「Php/laravel/laravel5/facebookログイン」の版間の差分
提供: 初心者エンジニアの簡易メモ
行59: | 行59: | ||
$fuser->getEmail() | $fuser->getEmail() | ||
); | ); | ||
− | print nl2br(print_r($ret,1)); | + | print nl2br(print_r($ret,1)); |
+ | print nl2br(print_r($fuser,1)); | ||
+ | // Laravel\Socialite\Two\User Object | ||
+ | // ( | ||
+ | // [token] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | ||
+ | // [refreshToken] => | ||
+ | // [expiresIn] => 1234567 | ||
+ | // [id] => 1128206012345678 | ||
+ | // [nickname] => | ||
+ | // [name] => name1 | ||
+ | // [email] => sample@example.com | ||
+ | // [avatar] => https://graph.facebook.com/v2.6/1128206012345678/picture?type=normal | ||
+ | // [user] => Array | ||
+ | // ( | ||
+ | // [name] => name1 | ||
+ | // [email] => sample@example.com | ||
+ | // [gender] => male | ||
+ | // [verified] => 1 | ||
+ | // [id] => 1128206012345678 | ||
+ | // ) | ||
+ | // [avatar_original] => https://graph.facebook.com/v2.6/1128206012345678/picture?width=1920 | ||
+ | // ) | ||
} | } | ||
} | } |
2016年8月5日 (金) 21:21時点における版
facebookAppのID取得
以下でAppIDとapp secretを取得 https://developers.facebook.com
ソーアシャルライブラリインストール
$ composer require laravel/socialite
コード追加
$ vi config/app.php 'providers' => [ Laravel\Socialite\SocialiteServiceProvider::class,
'aliases' => [ 'Socialite' => Laravel\Socialite\Facades\Socialite::class,
$ vi config/services.php 'facebook' => [ 'client_id' => env('FACEBOOK_ID'), 'client_secret' => env('FACEBOOK_SECRET'), 'redirect' => env('FACEBOOK_CALLBACKURL'), ],
$ vi .env FACEBOOK_ID=xxxxxxxxx FACEBOOK_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx FACEBOOK_CALLBACKURL=http://localhost/facebook/callback
$ vi app/Http/routes.php Route::group(['middleware' => 'web'], function () { Route::get('facebook','FacebookController@facebookLogin'); Route::get('facebook/callback','FacebookController@facebookCallback'); });
$ vi app/Http/Controller/FacebookController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use Laravel\Socialite\Contracts\Factory as Socialite; class FacebookController extends Controller { protected $socialite; public function __construct(Socialite $socialite) { $this->socialite = $socialite; } public function facebookLogin() { return $this->socialite->driver('facebook')->redirect(); } public function facebookCallback() { $fuser = $this->socialite->driver('facebook')->user(); $ret = array( $fuser->getId(), $fuser->getName(), $fuser->getEmail() ); print nl2br(print_r($ret,1)); print nl2br(print_r($fuser,1)); // Laravel\Socialite\Two\User Object // ( // [token] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // [refreshToken] => // [expiresIn] => 1234567 // [id] => 1128206012345678 // [nickname] => // [name] => name1 // [email] => sample@example.com // [avatar] => https://graph.facebook.com/v2.6/1128206012345678/picture?type=normal // [user] => Array // ( // [name] => name1 // [email] => sample@example.com // [gender] => male // [verified] => 1 // [id] => 1128206012345678 // ) // [avatar_original] => https://graph.facebook.com/v2.6/1128206012345678/picture?width=1920 // ) } }
facebook側で以下エラーが出る場合
URLを読み込めません: このURLのドメインはアプリのドメインに含まれていません。このURLを読み込むには、アプリ設定のアプリドメインにすべてのドメインとサブドメインを追加してください。
- facebook開発ページの設定/プラットフォーム追加でwebサイトを選択/ウェブURLにurlを追加
- facebook開発ページの設定/アプリドメインにhttpsから始まるurlを追加する