oriceon/oauth-5-laravel 㧠Pocket API ã® OAuth èªè¨¼ãæ¤è¨¼ããã¨ãã®ã¡ã¢
æ¤è¨¼ç°å¢
$ php --version PHP 5.5.19 (cli) (built: Nov 12 2014 12:35:44) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies $ php artisan --version Laravel Framework version 5.2.43
oriceon/oauth-5-laravel ã®ã¤ã³ã¹ãã¼ã«
composer.json ã«ä¸è¨ã追å
"require": { "oriceon/oauth-5-laravel": "dev-master" }
composer update ãå®è¡
$ composer update
consumer key ã®åå¾
ä¸è¨ãªã³ã¯å
ã§äºã consumer key ãåå¾ãã¾ã
Pocket: Developer API
ãµã³ãã«ã³ã¼ã
.env ã«ä¸è¨ã追å
# oauth POCKET_CLIENT_ID=Your-Consumer-Key
config/oauth-5-laravel.php
<?php return [ /* |-------------------------------------------------------------------------- | oAuth Config |-------------------------------------------------------------------------- */ /** * Storage */ 'storage' => '\\OAuth\\Common\\Storage\\Session', /** * Consumers */ 'consumers' => [ 'Pocket' => [ 'client_id' => env('POCKET_CLIENT_ID'), 'client_secret' => null, // Pocket API doesn't have a secret key. 'scope' => [], ], ] ];
app/Http/Controllers/PocketController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class PocketController extends Controller { public function loginOAuth(Request $request) { // get pocket service $pocketService = \OAuth::consumer('Pocket',url('/loginresult')); // get request token $reqToken = $pocketService->requestRequestToken(); $request->session()->put( 'code', $reqToken ); // get pocketService authorization $url = $pocketService->getAuthorizationUri([ 'request_token' => $reqToken ]); // return to pocket login url return redirect((string)$url); } public function loginResult(Request $request) { // get data from request $code = $request->session()->get('code'); // get pocket service $pocketService = \OAuth::consumer('Pocket',url('/loginresult')); // if code is provided get user data and sign in if ( ! is_null($code)) { // This was a callback request from pocket, get the token $token = $pocketService->requestAccessToken($code); $params = [ 'consumer_key' => env('POCKET_CLIENT_ID'), 'access_token' => $token->getAccessToken(), 'sort' => 'newest', 'count' => '1', ]; // get a post (test) $json_result = $pocketService->request('https://getpocket.com/v3/get', 'POST', json_encode($params), [ 'Content-Type' => 'application/json' ]); $result = json_decode( $json_result ); dd($result); } else { dd('code is null.'); } } }
app/Http/routes.php
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::get('login', 'PocketController@loginOAuth' ); Route::get('loginresult', 'PocketController@loginResult' );
autoload æ´æ°
$ php artisan optimize
åä½ç¢ºèª
ãã¹ããµã¼ãã¼èµ·å
$ php artisan serve
WEBãã©ã¦ã¶ã§ http://localhost:8000/login ãéãã¦ä¸ã®ç»åã®ããã«è¡¨ç¤ºãããã°æåã§ã