Asana Refresh token

I get asana access token and refresh token and save it in my database.
$client = Asana\Client::oauth(array(
‘client_id’ => $config[‘client_id’],
‘client_secret’ => $config[‘client_secret’],
‘redirect_uri’ => $this->getRedirectUri(),
));
$access_token = $client->dispatcher->fetchToken($access_token);
$token = $client->dispatcher->refreshAccessToken();
and then I use below code for get tasks from api. after 1hour time I get access token is not set error.
$client = Asana\Client::oauth(array(
‘client_id’ =>$config[‘client_id’],
‘client_secret’ => $config[‘client_secret’],
‘redirect_uri’ => $this->getRedirectUri(),
‘token’ => $token[‘access_token’],
‘refresh_token’ => $token[‘refresh_token’]
));

            $result = $client->get("/projects/{$projectId}/tasks?opt_fields=modified_at,name,notes&limit=1", []);

Please help me to retrieve life time token from asana and use that for further communications

If you want a token that lasts forever, you could simply use a PAT.

If you need to use oauth and refresh tokens, you can check for an Unauthorized error code, and trigger a refresh from that. I’m not sure if our client library does this for you out of the box.

Pseudo Code:

try { 
  $result = $client->get("/projects/{$projectId}/tasks?opt_fields=modified_at,name,notes&limit=1", []);
} catch ($e) {
  if ($e is Unauthorized) {
    $token = $client->dispatcher->refreshAccessToken();
    // Redo request
  }
}
1 Like