Created a PAT. But it does not work. Please help

I’ve created a PAT using the documentation API guide. Then I’ve tried to use the example-accesstoken.php from the PHP Github library.

Used the PAT in the code and I keep receiving the next error
“Please set the ASANA_ACCESS_TOKEN environment variable”

What should I do. I’ve already tried to create another PAT and it does not work no matter what I do.

Thanks

Hi @Oren_Sharabi and welcome to the forum,

You’ve created an environment variable called ASANA_ACCESS_TOKEN and set it to the value of your PAT, as that code example instructs?

If you have done that and are still getting that error, I don’t work in PHP but looking at the code example, it looks like that error occurs directly in the sample PHP code, not inside of the Asana PHP client library, so it seems it should be possible to debug the code and see why this IF statement is returning false:

$ASANA_ACCESS_TOKEN = getenv('ASANA_ACCESS_TOKEN');

if ($ASANA_ACCESS_TOKEN === false) {
    echo "Please set the ASANA_ACCESS_TOKEN environment variable.\n";
    exit;
}

If none of the above helps, then please post your code so a dev who works in PHP (there are definitely some around here) can take a look at it.

1 Like

Thanks.
Here is the code

getenv() — Gets the value of an environment variable.

There is no value set for the ASANA_ACCESS_TOKEN environment variable. So it will always return false as the environment variable varname does not exist.

In line 8 your current code provided is trying to get a value for an environment variable varname called 1/1200XXXXXXXXXXXXX.

Now environment variables are an excellent way to configure PHP applications because they keep the application’s settings outside of the code. By doing this, it’s easier to prevent secure credentials from being exposed, maintain applications, and use applications across multiple environments.

So to use it this way you have to set this value, assuming you are using an Apache Server your code will look like the following.

<?php

require dirname(__FILE__) . '/vendor/autoload.php'; // Change this to your correct file location

use Asana\Client;

apache_setenv('ASANA_ACCESS_TOKEN', '1/1200XXXXXXXXXXXXXX'); // Replace 1/1200XXXXXXXXXXXXXX with your PAT (Personal Access Token)

// $ASANA_ACCESS_TOKEN = getenv('ASANA_ACCESS_TOKEN');
$ASANA_ACCESS_TOKEN = getenv('ASANA_ACCESS_TOKEN');

// Access Token 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());

Now if you do not need this feature then just remove the getenv() function. So your code will look like the following.

<?php

require dirname(__FILE__) . '/vendor/autoload.php'; // Change this to your correct file location

use Asana\Client;

$ASANA_ACCESS_TOKEN = '1/1200XXXXXXXXXXXXXX'; // Replace 1/1200XXXXXXXXXXXXXX with your PAT (Personal Access Token)

// Access Token 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());
2 Likes

Apologies as with my second example there will be extra code that wont be needed such as the if statement. As in the second example setting the variable directly to the PAT without using the getenv() function, will never return false unless we specifically set the variables value to false. However it will push out an error though if the PAT is wrong.

<?php

require dirname(__FILE__) . '/vendor/autoload.php'; // Change this to your correct file location

use Asana\Client;

$ASANA_ACCESS_TOKEN = '1/1200XXXXXXXXXXXXXX'; // Replace 1/1200XXXXXXXXXXXXXX with your PAT (Personal Access Token)

// 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());
2 Likes

Thanks for jumping in here with the answer, @Shane8! You are now our officially designated PHP Expert! :tm:

1 Like

Thanks @Shane8 . It seems like it worked out. I can move on now :slight_smile:

1 Like