Created
November 5, 2013 20:24
-
-
Save muteor/7325603 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"adoy/oauth2": "dev-master" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Load composer autoloader | |
require 'vendor/autoload.php'; | |
const CLIENT_ID = 'xxx'; // OAuth 2.0 client_id | |
const CLIENT_SECRET = 'xxxx'; // OAuth 2.0 client_secret | |
const REDIRECT_URI = 'http://stashapi.local/submit-to-stash.php'; | |
const AUTHORIZATION_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/authorize'; | |
const TOKEN_ENDPOINT = 'https://www.deviantart.com/oauth2/draft15/token'; | |
const SUBMIT_API = "https://www.deviantart.com/api/draft15/stash/submit"; | |
const APPNAME = 'App.Name'; | |
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 = (object) $response['result']; | |
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); | |
$response = $client->fetch( | |
SUBMIT_API, | |
array( | |
'title' => 'Fella Sample Image', | |
'artist_comments' => 'Fella Sample Image', | |
'keywords' => 'fella sample image', | |
'folder' => APPNAME, | |
'file' => "@fella.png" | |
), | |
OAuth2\Client::HTTP_METHOD_POST | |
); | |
$result = (object) $response['result']; | |
if (!$result) { | |
throw new Exception('No valid JSON response returned'); | |
} | |
if ($result->status == 'success') { | |
print "Great Success! <a href=\"http://sta.sh/1{$result->stashid}\" target=\"_blank\">Stash ID {$result->stashid}</a>"; | |
} else { | |
throw new Exception($result->human_error); | |
} | |
} | |
} catch (Exception $e) { | |
print "Fatal Error: ".$e->getMessage(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment