PHP code example of brightleaf-digital / asana-client
1. Go to this page and download the library: Download brightleaf-digital/asana-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
brightleaf-digital / asana-client example snippets
use BrightleafDigital\AsanaClient;
$personalAccessToken = 'your-personal-access-token';
$client = AsanaClient::withPAT($personalAccessToken);
// Get user information
$me = $client->users()->getCurrentUser();
// Create a task
$taskData = [
'name' => 'My new task',
'notes' => 'Task description',
'projects' => ['12345678901234'] // Project GID
];
$task = $client->tasks()->createTask($taskData);
use BrightleafDigital\AsanaClient;
use BrightleafDigital\Auth\Scopes;
$clientId = 'your-client-id';
$clientSecret = 'your-client-secret';
$redirectUri = 'https://your-app.com/callback';
// Bootstrap with defaults (uses an internal container and file token storage)
$client = AsanaClient::OAuth($clientId, $clientSecret, $redirectUri, __DIR__ . '/token.json');
// Request specific scopes and get URL + state + PKCE verifier
$auth = $client->getSecureAuthorizationUrl([
Scopes::TASKS_READ,
Scopes::PROJECTS_READ,
Scopes::USERS_READ
]);
// Store $auth['state'] and $auth['codeVerifier'] in the session, then redirect to $auth['url']
// In your callback handler:
$code = $_GET['code'];
$pkceVerifier = $_SESSION['oauth2_pkce_verifier'] ?? null;
$client->handleCallback($code, $pkceVerifier); // Token is saved automatically via TokenManager
// Then use the client
$me = $client->users()->getCurrentUser();
use BrightleafDigital\AsanaClient;
// Initialize the client (automatically loads/saves to token.json)
$asanaClient = AsanaClient::OAuth($clientId, $clientSecret, $redirectUri, __DIR__ . '/token.json', null, $salt);
// Optional: Subscribe to refresh events for additional logging or custom logic
$asanaClient->subscribeToTokenRefresh(function ($newToken) {
// Note: You DON'T need to call save() here; the library already did it!
echo "Token refreshed successfully!";
});
// Example API call that triggers a token refresh if the token is expired
$userInfo = $asanaClient->users()->getCurrentUser();
// Use memory storage (false) so no files are written
$client = AsanaClient::withAccessToken($clientId, $clientSecret, $tokenData, false);
// Subscribe to refresh events to keep your DB in sync
$client->subscribeToTokenRefresh(function($newToken) use ($db) {
$db->update('users')->set(['token' => json_encode($newToken)])->execute();
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.