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

takaya030の備忘録

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

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

前回手動で Google API の Access Token と Refresh Token を取得したので、それを使って oriceon/oauth-5-laravel で 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.45

oriceon/oauth-5-laravel のインストール

composer.json に下記を追加

    "require": {
	"oriceon/oauth-5-laravel": "dev-master"
    },

composer update を実行

$ composer update

OAuth クライアント ID、Refresh Token の取得

前回の記事を参考に予め取得する
Google API の Access Token を手動で取得する - takaya030の備忘録

サンプルコード

.env に下記を追加

# oauth
GOOGLE_CLIENT_ID=Your-Clinet-Id
GOOGLE_CLIENT_SECRET=Your-Clinet-Secret
GOOGLE_ACCESS_TOKEN=Your-Access-Token
GOOGLE_REFRESH_TOKEN=Your-Refresh-Token

config/oauth-5-laravel.php

<?php

return [

	/*
	|--------------------------------------------------------------------------
	| oAuth Config
	|--------------------------------------------------------------------------
	*/

	/**
	 * Storage
	 */
	'storage' => '\\OAuth\\Common\\Storage\\Session',

	/**
	 * Consumers
	 */
	'consumers' => [

		'Google' => [
			'client_id'     => env('GOOGLE_CLIENT_ID'),
			'client_secret' => env('GOOGLE_CLIENT_SECRET'),
			'scope'         => ['https://mail.google.com/'],
		],

	]

];

app/Http/Controllers/GmailController.php

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use OAuth\OAuth2\Token\StdOAuth2Token;

class GmailController extends Controller
{
	public function loginOAuth(Request $request)
	{
		// get data from request
		$access_token = env("GOOGLE_ACCESS_TOKEN");
		$refresh_token = env("GOOGLE_REFRESH_TOKEN");

		// get google service
		$googleService = \OAuth::consumer('Google');

		if( !is_null($access_token) && !is_null($refresh_token) )
		{
			$dummy_token = new StdOAuth2Token( $access_token, $refresh_token );
			$token = $googleService->refreshAccessToken($dummy_token);

			$params = [
				'maxResults' => '3',
				'q' => 'is:inbox',
			];

			// Send a request with it
			$result = json_decode($googleService->request('https://www.googleapis.com/gmail/v1/users/me/messages?'.http_build_query($params),'GET'), true);

			dd($result);
		}
		else
		{
			dd('No Access Token or Refresh Token.');
		}
	}

}

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('gmail', 'GmailController@loginOAuth' );

autoload 更新

$ php artisan optimize

動作確認

テストサーバー起動

$ php artisan serve

WEBブラウザで http://localhost:8000/gmail を開いて下の画像のように表示されれば成功です
f:id:takaya030:20160904174005p:plain