前回手動で 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 ãéãã¦ä¸ã®ç»åã®ããã«è¡¨ç¤ºãããã°æåã§ã