読者です 読者をやめる 読者になる 読者になる

takaya030の備忘録

PHP、Laravel、Docker などの話がメインです

oriceon/oauth-5-laravel を使った Pocket API の OAuth 認証

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 を開いて下の画像のように表示されれば成功です
f:id:takaya030:20160830232658p:plain