Laravel 㧠Tumblr API ã®ã¢ã¯ã»ã¹ãã¼ã¯ã³ãåå¾ããããã°ã©ã ãåãããã¨ãã®ã¡ã¢
æ¤è¨¼ç°å¢
$ php --version PHP 7.1.4 (cli) (built: May 11 2017 17:22:31) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies $ php artisan --version Laravel Framework 5.4.33
Tumblr API ã®èªè¨¼ã«ã¤ãã¦ã®æ³¨æç¹
- èªè¨¼ãããã³ã«ã¯ OAuth 1.0a
- Twitter ãªã©ã¨éããéçºã³ã³ã½ã¼ã«ç»é¢ã§ Access Token ã®ç¢ºèªãã§ããªããããWeb ã¢ããªã±ã¼ã·ã§ã³ãªã©ã§ Access Token ãå¿ è¦ãªå ´åã¯èªè¨¼ããã°ã©ã ã使ã£ã¦åå¾ããå¿ è¦ããã
OAuth ããã±ã¼ã¸ã«ã¤ãã¦
ä»å㯠Laravel5 対å¿ã®ä»¥ä¸ã®ããã±ã¼ã¸ã使ç¨ãã¾ã
github.com
oriceon/oauth-5-laravel ã®ã¤ã³ã¹ãã¼ã«
composer.json ã® require ã»ã¯ã·ã§ã³ã«ä¸è¨å 容ã追å
"require": { "oriceon/oauth-5-laravel": "dev-master" }
ãã®å¾ composer update ã§ã¤ã³ã¹ãã¼ã«
$ composer update
config/app.php ãä¸è¨ã®ããã«å¤æ´ãã¦ããã±ã¼ã¸ãç»é²
--- config/app.php.orig Fri Aug 18 12:25:23 2017 +++ config/app.php Wed Aug 16 16:55:38 2017 @@ -177,6 +177,11 @@ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, + /* + * OAuth + */ + Artdarek\OAuth\OAuthServiceProvider::class, + ], /* @@ -212,6 +217,7 @@ 'Log' => Illuminate\Support\Facades\Log::class, 'Mail' => Illuminate\Support\Facades\Mail::class, 'Notification' => Illuminate\Support\Facades\Notification::class, + 'OAuth' => Artdarek\OAuth\Facade\OAuth::class, 'Password' => Illuminate\Support\Facades\Password::class, 'Queue' => Illuminate\Support\Facades\Queue::class, 'Redirect' => Illuminate\Support\Facades\Redirect::class,
以ä¸ã®å 容㧠config/oauth-5-laravel.php ãä½æ
<?php return [ /* |-------------------------------------------------------------------------- | oAuth Config |-------------------------------------------------------------------------- */ /** * Storage */ 'storage' => '\\OAuth\\Common\\Storage\\Session', /** * Consumers */ 'consumers' => [ 'Tumblr' => [ 'client_id' => env('TUMBLR_CLIENT_ID'), 'client_secret' => env('TUMBLR_CLIENT_SECRET'), // No scope - oauth1 doesn't need scope ], ] ];
.env ã« Consumer Key, Consumer Secret ã追å
TUMBLR_CLIENT_ID=Your-Consumer-Key TUMBLR_CLIENT_SECRET=Your-Consumer-Secret
OAuth èªè¨¼ããã°ã©ã
app/Http/Controllers/TumblrAuthController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class TumblrAuthController extends Controller { public function loginWithTumblr(Request $request) { // get data from request $token = $request->get('oauth_token'); $verify = $request->get('oauth_verifier'); // get tumblr service $tmb = \OAuth::consumer('Tumblr'); // check if code is valid // if code is provided get user data and sign in if ( ! is_null($token) && ! is_null($verify)) { // This was a callback request from tumblr, get the token $token = $tmb->requestAccessToken($token, $verify); //Var_dump //display whole array. dd($token); } // if not ask for permission first else { // get request token $reqToken = $tmb->requestRequestToken(); // get Authorization Uri sending the request token $url = $tmb->getAuthorizationUri(['oauth_token' => $reqToken->getRequestToken()]); // return to tumblr login url return redirect((string)$url); } } }
routes/web.php
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/tumblr', 'TumblrAuthController@loginWithTumblr');
OAuth èªè¨¼ããã°ã©ã ã®å®è¡
以ä¸ã®ã³ãã³ãã§éçºç¨ãµã¼ãã¼ãèµ·åãã¾ã
$ php artisan serve
web ãã©ã¦ã¶ã§ http://127.0.0.1:8000/tumblr ã«ã¢ã¯ã»ã¹ãã¾ã
éä¸ã以ä¸ã®ç»é¢ã表示ãããã¨ã㯠Allow ãã¯ãªãã¯ãã¾ã
web ãã©ã¦ã¶ã«ä»¥ä¸ã®ãã㪠Access Token ã®è¡¨ç¤ºãåºãã°æåã§ã
StdOAuth1Token {#175 â¼ #requestToken: "xxxxx...." #requestTokenSecret: "xxxxx..." #accessTokenSecret: "xxxxx..." #accessToken: "xxxxx..." #refreshToken: null #endOfLife: -9002 #extraParams: [] }