<?php require dirname(__FILE__) . '/../vendor/autoload.php'; use Asana\Client; $ASANA_API_KEY = getenv('ASANA_API_KEY'); // API Key Instructions: // 1. set your ASANA_API_KEY environment variable to the API key found in Asana Account Settings if ($ASANA_API_KEY === false) { echo "Please set the ASANA_API_KEY environment variable.\n"; exit; } // create a $client->with your Asana API key $client = Asana\Client::basicAuth($ASANA_API_KEY); $me = $client->users->me(); echo "me="; var_dump($client->users->me()); // find your "Personal Projects" project $personalProjectsArray = array_filter($me->workspaces, function ($item) { return $item->name === 'Personal Projects'; }); $personalProjects = array_pop($personalProjectsArray); var_dump($personalProjects); $projects = $client->projects->findByWorkspace($personalProjects->id, null, array('iterator_type' => false, 'page_size' => null))->data; echo "personal projects="; var_dump($projects); // create a "demo project" if it doesn't exist $projectArray = array_filter($projects, function ($project) { return $project->name === 'demo project'; }); $project = array_pop($projectArray); if ($project === null) {
function client($token = null) { return Asana\Client::oauth(array('client_id' => getenv('ASANA_CLIENT_ID'), 'client_secret' => getenv('ASANA_CLIENT_SECRET'), 'redirect_uri' => 'http://localhost:5000/auth/asana/callback', 'token' => $token)); }
<?php require dirname(__FILE__) . '/../vendor/autoload.php'; use Asana\Client; $ASANA_ACCESS_TOKEN = getenv('ASANA_ACCESS_TOKEN'); // API Key Instructions: // 1. set your ASANA_ACCESS_TOKEN environment variable to a Personal Access Token found in Asana Account Settings if ($ASANA_ACCESS_TOKEN === false) { echo "Please set the ASANA_ACCESS_TOKEN environment variable.\n"; exit; } echo "== Example using Personal Access Token:\n"; // create a $client->with a Personal Access Token $client = Asana\Client::accessToken($ASANA_ACCESS_TOKEN); $me = $client->users->me(); echo "me="; var_dump($client->users->me());
private static function bootstrap() { if (Client::$ALL_OPTIONS == null) { Client::$CLIENT_OPTIONS = array_keys(Client::$DEFAULTS); Client::$ALL_OPTIONS = array_merge(Client::$CLIENT_OPTIONS, Client::$QUERY_OPTIONS, Client::$REQUEST_OPTIONS, Client::$API_OPTIONS); } }
// create a $client->with the OAuth credentials: $client = Asana\Client::oauth(array('client_id' => getenv('ASANA_CLIENT_ID'), 'client_secret' => getenv('ASANA_CLIENT_SECRET'), 'redirect_uri' => Asana\Dispatcher\OAuthDispatcher::NATIVE_REDIRECT_URI)); echo "authorized=" . $client->dispatcher->authorized . "\n"; # get an authorization URL: $state = null; $url = $client->dispatcher->authorizationUrl($state); try { // in a web app you'd redirect the user to this URL when they take action to // login with Asana or connect their account to Asana exec("open " . escapeshellarg($url)); } catch (Exception $e) { echo "Open the following URL in a browser to authorize:\n"; echo "{$url}\n"; } echo "Copy and paste the returned code from the browser and press enter:\n"; $code = trim(fgets(fopen("php://stdin", "r"))); // exchange the code for a bearer token $token = $client->dispatcher->fetchToken($code); echo "authorized=" . $client->dispatcher->authorized . "\n"; echo "token=" . json_encode($token) . "\n"; echo "me="; var_dump($client->users->me()); // normally you'd persist this token somewhere $token = json_encode($token); // (see below) // demonstrate creating a client using a previously obtained bearer token echo "== Example using OAuth Access Token:\n"; $client = Asana\Client::oauth(array('client_id' => $ASANA_CLIENT_ID, 'token' => json_decode($token))); echo "authorized=" . $client->dispatcher->authorized . "\n"; echo "me="; var_dump($client->users->me());