diff options
author | Aaron Parecki <aaron@parecki.com> | 2014-12-25 18:50:34 -0800 |
---|---|---|
committer | Aaron Parecki <aaron@parecki.com> | 2014-12-25 18:50:34 -0800 |
commit | 430609b9005bb93ce2c6b21405cea0ede2b17a9d (patch) | |
tree | 090b64c2242ba5e8bbe8342a9a9494dcbeb1bf41 | |
parent | 2cd148c792a47fea18d760b723d23569ae52d390 (diff) |
adds instagram auth
-rw-r--r-- | composer.json | 3 | ||||
-rw-r--r-- | composer.lock | 45 | ||||
-rw-r--r-- | controllers/controllers.php | 40 | ||||
-rw-r--r-- | lib/helpers.php | 11 | ||||
-rw-r--r-- | 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,9 +3,52 @@ "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", "target-dir": "Firebase/PHP-JWT", 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 @@ <h3>Twitter</h3> <input type="button" id="twitter-button" value="Checking" class="btn"> - - <!-- <h3>Instagram</h3> - - --> + <input type="button" id="instagram-button" value="Checking" class="btn"> </div> <script> @@ -60,6 +57,10 @@ function save_facebook_token(token) { } $(function(){ + $("#facebook-button").click(function(){ + FB.login(window.quillHandleFbLogin, {scope:'publish_actions'}); + }); + $.getJSON("/auth/twitter", function(data){ // Check if we're already authorized with twitter if(data && data.result == 'ok') { @@ -81,8 +82,26 @@ $(function(){ } }); - $("#facebook-button").click(function(){ - FB.login(window.quillHandleFbLogin, {scope:'publish_actions'}); + $.getJSON("/auth/instagram", function(data){ + // Check if we're already authorized with Instagram + if(data && data.result == 'ok') { + $("#instagram-button").val("Connected").addClass("btn-success"); + } else if(data && data.url) { + $("#instagram-button").val("Sign In").data("url", data.url).addClass("btn-warning"); + } else { + $("#instagram-button").val("Error").addClass("btn-danger"); + } + }); + + $("#instagram-button").click(function(){ + if($(this).data('url')) { + window.location = $(this).data('url'); + } else { + $.getJSON("/auth/instagram", {login: 1}, function(data){ + window.location = data.url; + }); + } }); + }); </script> |