Skip to content

Instantly share code, notes, and snippets.

@andrejko
Last active December 15, 2015 04:19
Show Gist options
  • Save andrejko/5200647 to your computer and use it in GitHub Desktop.
Save andrejko/5200647 to your computer and use it in GitHub Desktop.
Oauth for DeviantArt
<?php
//Relies on the oAuth2 library by Pierrick Charron: https://github.com/adoy/PHP-OAuth2/
const CLIENT_ID = '***'; // OAuth 2.0 client_id
const CLIENT_SECRET = '***'; // OAuth 2.0 client_secret
const REDIRECT_URI = 'http://getcoolphoto.com/site/deviant';
const AUTHORIZATION_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/authorize';
const TOKEN_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/token';
const SUBMIT_API = "http://www.deviantart.com/api/draft15/stash/submit";
const APPNAME = 'getcoolphoto';
if (empty($_SESSION['deviant_access_token'])) {
Yii::import('application.components.OAuth2.*');
require('Client.php');
Yii::import('application.components.OAuth2.GrantType.*');
require('IGrantType.php');
require('ClientCredentials.php');
require('Password.php');
require('RefreshToken.php');
require('AuthorizationCode.php');
try {
$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET, OAuth2\Client::AUTH_TYPE_AUTHORIZATION_BASIC);
if (!isset($_REQUEST['code'])) {
$params = array('redirect_uri' => REDIRECT_URI);
$auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
header('Location: ' . $auth_url);
die('Redirecting ...');
} else {
$params = array('code' => $_REQUEST['code'], 'redirect_uri' => REDIRECT_URI);
$response = $client->getAccessToken(TOKEN_ENDPOINT, OAuth2\Client::GRANT_TYPE_AUTH_CODE, $params);
$val = $response['result'];
if (!$val) {
throw new Exception('No valid JSON response returned');
}
if (!$val['access_token']) {
throw new Exception("No access token returned: ".$val['human_error']);
}
$client->setAccessToken($val['access_token']);
$client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_OAUTH);
$_SESSION['deviant_access_token'] = $val['access_token'];
$this->redirect('/site/deviant');
}
} catch (Exception $e) {
print "Fatal Error: ".$e->getMessage();
}
} else {
?>
<form action="https://www.deviantart.com/api/draft15/stash/submit?access_token=<?php echo $_SESSION['deviant_access_token']; ?>" method="POST" enctype="multipart/form-data">
<input type="file" name="photo">
<input type="submit" value="GO">
</form>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment