Skip to content

Instantly share code, notes, and snippets.

@daiki44
Created August 6, 2017 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daiki44/df942e76cfaee63deae71f9e56811bf7 to your computer and use it in GitHub Desktop.
Save daiki44/df942e76cfaee63deae71f9e56811bf7 to your computer and use it in GitHub Desktop.
LaravelでInstagramでログインをやってみた ref: http://qiita.com/daiki_44/items/6d01029cd3a7e7111c9a
INSTAGRAM_KEY=`自分のAppのClient ID`
INSTAGRAM_SECRET=`自分のAppのClient Secret`
INSTAGRAM_REDIRECT_URI=`自分のHOST`/instagram/callback/
<?php
return [
'client_id' => env('INSTAGRAM_KEY'),
'client_secret' => env('INSTAGRAM_SECRET'),
'callback_url' => env('INSTAGRAM_REDIRECT_URI'),
];
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use MetzWeb\Instagram\Instagram;
/**
* Instagramでログインを行う際のController
*/
class InstagramController extends Controller
{
# 共通で使うので、constructに格納
private $instagram;
function __construct() {
# configからAppの値を取得し、インスタンス化
$this->instagram = new Instagram(array(
'apiKey' => config('instagram.client_id'),
'apiSecret' => config('instagram.client_secret'),
'apiCallback' => config('instagram.callback_url')
));
}
/**
* Instagramで登録、ログインを押された際の処理
*/
public function instagramLogin() {
return redirect($this->instagram->getLoginUrl(array(
# アクセスする権限をここで指定可能
# ただし、対応していない権限も存在しているので、その場合はURLを生で書くようにする
'basic'
)));
}
/**
* Instagramで登録、ログインを押された後にリダイレクトされた際の処理
* Instagramの情報を取得できる
*/
public function instagramCallback(Request $request) {
# URLにcodeが入っているので取得
$code = $request->code;
# 取得したcodeを利用し、OAhtu認証
$data = $this->instagram->getOAuthToken($code);
$this->instagram->setAccessToken($data);
# OAuth認証が完了したので、$instagram->"method"で好きなAPIが叩けるようになっている
# 今回はInstagramでログイン機能なので、getUser()を使用
$user_data = $this->instagram->getUser();
echo '<pre>';
var_dump($user_data);
echo '</pre>';
}
}
composer require cosenary/instagram
<a href="{{url('/instagram/')}}">
Instagramでログイン・登録
</a>
php artisan make:controller InstagramController
# Instagram login
Route::get('/instagram/', 'InstagramController@instagramLogin');
# Instagram callback
Route::get('/instagram/callback/', 'InstagramController@instagramCallback');
object(stdClass)#154 (2) {
["data"]=>
object(stdClass)#155 (7) {
["id"]=>
string(9) "223689238"
["username"]=>
string(15) "daiki.sekiguchi"
["profile_picture"]=>
string(105) "https://scontent.cdninstagram.com/t51.2885-19/s150x150/16789727_236142133514690_5385687353354354688_a.jpg"
["full_name"]=>
string(15) "Daiki Sekiguchi"
["bio"]=>
string(0) ""
["website"]=>
string(0) ""
["counts"]=>
object(stdClass)#156 (3) {
["media"]=>
int(2)
["follows"]=>
int(60)
["followed_by"]=>
int(56)
}
}
["meta"]=>
object(stdClass)#157 (1) {
["code"]=>
int(200)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment