Skip to content

Instantly share code, notes, and snippets.

@jamwaffles
Created January 8, 2014 14:52
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 jamwaffles/8317956 to your computer and use it in GitHub Desktop.
Save jamwaffles/8317956 to your computer and use it in GitHub Desktop.
Imgur API class
<?php
namespace Model;
use RedBean_Facade as R;
class ImgurAPI extends \Pliers\Model {
protected $user;
protected $session;
public function __construct() {
parent::__construct();
$this->session = new \Model\DBSession;
$this->user = $this->session->getUser();
}
// Talk to Imgur API for authorization to use Imgurian app
public function authorize() {
$this->app->redirect($this->conf->imgurApi->authUrl . '?client_id=' . $this->conf->imgurApi->clientId . '&response_type=code');
}
public function processApiCallback($code) {
if(!$code) {
throw new \InvalidArgumentException('No response code passed to callback');
}
$authorizeParams = array(
'client_id' => $this->conf->imgurApi->clientId,
'client_secret' => $this->conf->imgurApi->clientSecret,
'grant_type' => 'authorization_code',
'code' => $code
);
$request = new \Curl;
$response = $request->post($this->conf->imgurApi->tokenUrl, $authorizeParams);
if((int)$response->headers['Status-Code'] === 200) {
$body = json_decode($response->body);
// Get user data
$userData = json_decode($this->apiGet('/account/me', array(), $body->access_token)->body)->data;
$existingUser = R::findOne('user', " imgur_id = ?", array($userData->id));
if($existingUser) {
$this->user = $existingUser;
} else {
$this->user = R::dispense('user');
$this->user->access_token = $body->access_token;
$this->user->imgur_expires = date('Y-m-d H:i:s', time() + (int)$body->expires_in);
// Get email address. What a pointless extra API call.
$email = json_decode($this->apiGet('/account/me/settings')->body)->data->email;
$this->user->username = $userData->url;
$this->user->email = $email;
$this->user->imgur_id = $userData->id;
$this->user->imgur_created = $userData->created;
$this->user->created = date('Y-m-d H:i:s');
$this->session->destroy();
}
$this->user->access_token = $body->access_token;
$this->user->imgur_expires = date('Y-m-d H:i:s', time() + (int)$body->expires_in);
$this->user->updated = date('Y-m-d H:i:s');
$this->user->refresh_token = $body->refresh_token;
// Create session
$this->session->setUser($this->user);
R::store($this->user);
} else {
$resp = json_decode($response->body)->data;
$this->session->destroy();
throw new \RuntimeException('Could not access Imgur API. Reason: ' . $resp->error . ' (code ' . $response->headers['Status-Code'] . ')');
return false;
}
return true;
}
// Reauthenticate user with Imgur API.
public function reauth() {
$authParams = array(
'refresh_token' => $this->user->refresh_token,
'client_id' => $this->conf->imgurApi->clientId,
'client_secret' => $this->conf->imgurApi->clientSecret,
'grant_type' => 'refresh_token'
);
$request = new \Curl;
try {
$response = $request->post($this->conf->imgurApi->tokenUrl, $authParams);
} catch(\CurlException $e) {
throw new \RuntimeException('Could not connect to Imgur API: ' . $e->getMessage);
}
if((int)$response->headers['Status-Code'] === 200) {
$body = json_decode($response->body);
$this->user->refresh_token = $body->refresh_token;
$this->user->access_token = $body->access_token;
$this->user->imgur_expires = date('Y-m-d H:i:s', time() + (int)$body->expires_in);
$this->user->updated = date('Y-m-d H:i:s');
R::store($this->user);
return $this->user;
} else {
$resp = json_decode($response->body)->data;
throw new \RuntimeException('Could not reauthenticate with Imgur API. Reason: ' . $resp->error . ' (code ' . $response->headers['Status-Code'] . ')');
return false;
}
}
public function apiGet($path, $params = array(), $access_token = null) {
// If Imgur API key has timed out, reauth
if($this->user && strtotime($this->user->imgur_expires) < time()) {
$this->reauth();
}
$req = new \Curl;
$url = 'https://api.imgur.com/3/' . ltrim($path, '/');
$req->headers = array('Authorization' => 'Bearer ' . ($access_token !== null ? $access_token : $this->user->access_token));
return $req->get($url, $params);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment