Skip to content

Instantly share code, notes, and snippets.

@andrejko
Last active December 15, 2015 04:19

Revisions

  1. andrejko renamed this gist Jul 2, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. andrejko renamed this gist Jul 2, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. andrejko created this gist Mar 19, 2013.
    61 changes: 61 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    <?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
    }
    ?>