Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

robel93gm@gmail.com's avatar

my oauth2 access token is overwritten every time

So in my laravel calender app. When you make a booking in the clander iam requesting data to an environment api. I send in parameters like... 'driving distance', 'type_of_vehicle', 'load_weight' ect and get like 'co2' values and such in response.

You fetch data from the api via oauth2 access token. And in my app code i get a new access_token everytime iam requesting the data. which is about max 50 times a day. I dont check expire time and such. I havent really understand why it would be necessary in my scenario. The password and username is the same everytime and is hardcoded in my "getaccesstoken" method.

Is this bad. Ive been told this is bad and you have to check expire data and so but dont get why.

Could somebody explain to me please

this is my code to get access token

           $token = "Bearer ";
        $user_name = "*********";
        $password = "***********";
        $client_secret = "*********";
        $client_id = "*********";
        $authorization = base64_encode("$client_id:$client_secret");  
        $parameters = "grant_type=password&username=" . $user_name. "&password=" . $password;     
        $TokenEndPointPath = "**************************************";
        $header = array(
            "Authorization: Basic {$authorization}",
            'Content-Type: application/x-www-form-urlencoded'
        );

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $TokenEndPointPath);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
        $response = curl_exec($curl);
        curl_close($curl);

        $token_array = json_decode($response, true);
        if(isset($token_array["error"])){
            return "NTM Access Token failure - " . $token_array["error"] . "--->" . $token_array["error_description"];
        }
     
        return $token . $token_array["access_token"]; 
0 likes
0 replies

Please or to participate in this conversation.