unset($token_to_send['refresh_token']);
        unset($token_to_send['token_type']);
        echo json_encode($token_to_send);
    } else {
        echo '-1';
    }
    exit;
}
$envato = new envato_api_basic();
$envato->set_client_id(_ENVATO_APP_ID);
$envato->set_client_secret(_ENVATO_APP_SECRET);
$envato->set_redirect_url(_ENVATO_APP_URL);
if (isset($_REQUEST['refresh_token'])) {
    header("Content-type: text/javascript");
    if (!empty($_SESSION['oauth_token']['refresh_token']) && $_REQUEST['oauth_nonce'] == $_SESSION['oauth_nonce']) {
        $envato->set_manual_token($_SESSION['oauth_token']);
        $new_access = $envato->refresh_token();
        if ($new_access) {
            $_SESSION['oauth_token']['access_token'] = $new_access;
        }
        echo json_encode(array('new_token' => $new_access));
    } else {
        echo '-1';
    }
    exit;
}
if (!empty($_REQUEST['code'])) {
    // we have a login callback.
    $token = false;
    try {
        $token = $envato->get_authentication($_REQUEST['code']);