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';
$asanaClient = AsanaClient::withPersonalAccessToken($personalAccessToken);
// Get user information
$me = $asanaClient->users()->me();
// Create a task
$taskData = [
'name' => 'My new task',
'notes' => 'Task description',
'projects' => ['12345678901234'] // Project GID
];
$task = $asanaClient->tasks()->createTask($taskData);
use BrightleafDigital\AsanaClient;
use BrightleafDigital\Auth\Scopes;
$clientId = 'your-client-id';
$clientSecret = 'your-client-secret';
$redirectUri = 'https://your-app.com/callback';
// Create a client and get the authorization URL
$asanaClient = new AsanaClient($clientId, $clientSecret, $redirectUri);
// Option 1: Request specific scopes
$authUrl = $asanaClient->getAuthorizationUrl([
Scopes::TASKS_READ,
Scopes::PROJECTS_READ,
Scopes::USERS_READ
]);
// Option 2: Use default/full access (pass an empty array). May not be supported after July 2025.
// $authUrl = $asanaClient->getAuthorizationUrl([]);
// Redirect the user to $authUrl
// After authorization, Asana will redirect back to your callback URL
// In your callback handler:
$code = $_GET['code'];
$tokenData = $asanaClient->handleCallback($code);
// Save $tokenData for future use
// Then use the client
$workspaces = $asanaClient->users()->getCurrentUser();
use BrightleafDigital\AsanaClient;
$salt = 'your-secure-salt';
// Initialize the client with a stored token
$asanaClient = AsanaClient::withAccessToken('client-id', 'client-secret', AsanaClient::retrieveToken($salt));
// Subscribe to the 'token refreshed' event
$asanaClient->onTokenRefresh(function (array $token) use ($asanaClient, $salt) {
// Save the refreshed token securely
$asanaClient->saveToken($salt);
// Optional: Log or process the refreshed token
echo "Token refreshed successfully!";
});
// Example API call that triggers a token refresh if the token is expired
$userInfo = $asanaClient->users()->me();
use BrightleafDigital\AsanaClient;
use BrightleafDigital\Utils\CryptoUtils;
// Retrieve the access token for custom handling
$tokenArray = $asanaClient->getAccessToken();
// Encrypt the token manually
$salt = 'your-secure-salt';
$encryptedToken = CryptoUtils::encrypt(json_encode($tokenArray), $salt); // or just encrypt $tokenArray['access_token'] and $tokenArray['refresh_token']
// Store the encrypted token in a database or a secure location
storeTokenInDatabase($encryptedToken);
// Later: Load and decrypt the token
$storedToken = retrieveTokenFromDatabase();
$tokenData = json_decode(CryptoUtils::decrypt($storedToken, $salt), true);
// Initialize the client with the decrypted token
$asanaClient = AsanaClient::withAccessToken('client-id', 'client-secret', $tokenData);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.