From 430609b9005bb93ce2c6b21405cea0ede2b17a9d Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Thu, 25 Dec 2014 18:50:34 -0800 Subject: [PATCH] adds instagram auth --- composer.json | 3 ++- composer.lock | 45 ++++++++++++++++++++++++++++++++++++- controllers/controllers.php | 40 +++++++++++++++++++++++++++++++++ lib/helpers.php | 11 +++++++++ views/settings.php | 31 ++++++++++++++++++++----- 5 files changed, 122 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index a7dbea7..cf2f57e 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ "indieauth/client": "0.1.3", "mpratt/relativetime": ">=1.0", "firebase/php-jwt": "dev-master", - "ruudk/twitter-oauth": "dev-master" + "ruudk/twitter-oauth": "dev-master", + "andreyco/instagram": "3.*" }, "autoload": { "files": [ diff --git a/composer.lock b/composer.lock index 0bdac28..eb3dfc1 100644 --- a/composer.lock +++ b/composer.lock @@ -3,8 +3,51 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "502847c033f5a54c69a6a1a51d26e894", + "hash": "f2f8fdb671b52ce22dc0a5e133f9a13d", "packages": [ + { + "name": "andreyco/instagram", + "version": "v3.2", + "source": { + "type": "git", + "url": "https://github.com/Andreyco/Instagram-for-PHP.git", + "reference": "726e724c51301410873d9bae9c659a0597ca47e4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Andreyco/Instagram-for-PHP/zipball/726e724c51301410873d9bae9c659a0597ca47e4", + "reference": "726e724c51301410873d9bae9c659a0597ca47e4", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "php": ">=5.3.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Andreyco\\Instagram\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-4-Clause" + ], + "authors": [ + { + "name": "Andrej Badin", + "email": "contact@andrejbadin.com", + "homepage": "http://andrejbadin.com" + } + ], + "description": "An easy-to-use PHP Class for accessing Instagram's API.", + "homepage": "https://github.com/Andreyco/Instagram", + "keywords": [ + "api", + "instagram" + ], + "time": "2014-07-14 19:53:19" + }, { "name": "firebase/php-jwt", "version": "dev-master", diff --git a/controllers/controllers.php b/controllers/controllers.php index 18da149..47fd614 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -333,3 +333,43 @@ $app->get('/auth/twitter/callback', function() use($app) { $app->redirect('/settings'); } }); + +$app->get('/auth/instagram', function() use($app) { + if($user=require_login($app, false)) { + + $instagram = instagram_client(); + + // If there is an existing Instagram auth token, check if it's valid + if($user->instagram_access_token) { + $instagram->setAccessToken($user->instagram_access_token); + $igUser = $instagram->getUser(); + + if($igUser && $igUser->meta->code == 200) { + $app->response()->body(json_encode(array( + 'result' => 'ok', + 'username' => $igUser->data->username + ))); + return; + } + } + + $app->response()->body(json_encode(array( + 'result' => 'error', + 'url' => $instagram->getLoginUrl(array('basic','likes')) + ))); + } +}); + +$app->get('/auth/instagram/callback', function() use($app) { + if($user=require_login($app)) { + $params = $app->request()->params(); + + $instagram = instagram_client(); + $data = $instagram->getOAuthToken($params['code']); + $user->instagram_access_token = $data->access_token; + $user->save(); + + $app->redirect('/settings'); + } +}); + diff --git a/lib/helpers.php b/lib/helpers.php index 010bd91..456fcfd 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -195,3 +195,14 @@ function relative_time($date) { } return $rel->timeAgo($date); } + +function instagram_client() { + return new Andreyco\Instagram\Client(array( + 'apiKey' => Config::$instagramClientID, + 'apiSecret' => Config::$instagramClientSecret, + 'apiCallback' => Config::$base_url . 'auth/instagram/callback', + 'scope' => array('basic','likes'), + )); +} + + diff --git a/views/settings.php b/views/settings.php index 61feefc..9860a91 100644 --- a/views/settings.php +++ b/views/settings.php @@ -10,11 +10,8 @@

Twitter

- - +