Search code examples
phpapioauthoauth-2.0access-token

oath2 access token API response "expires_on" date not valid


I'm checking my access token's expiry date against the current date in London. I'm just trying to convert the time/date at this stage. Could someone please explain to me why the expiry date in my response is prior to generating the access token? I'm running it in http://phpfiddle.org/.

I'm calling this API: https://login.microsoftonline.com/"ID"/oauth2/token

And getting this response: "expires_on": "1596726056",

and then believe I'm converting it from epoch to human readable:

$expires = var_dump($tokenResponse_data->{'expires_on'});

var_dump(date('Y-m-d h:i:s', $expires));

But get this result, even though I only generated the token today: "1969-12-31 07:00:00".

Here is the full code, minus my credentials:

    <?php

    $curl_handle = curl_init();
    
    $tokenUrl = "https://login.microsoftonline.com/"app id"/oauth2/token";
    
    curl_setopt($curl_handle, CURLOPT_URL, $tokenUrl);
    
    curl_setopt($curl_handle, CURLOPT_POST, 1);
    
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS,
            "grant_type=client_credentials&client_id="id"&client_secret="client secret"--r~-PWm1&scope=https://graph.microsoft.com/Calendars.Read");
    
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    
    $curl_tokenData = curl_exec($curl_handle);
    
    curl_close($curl_handle);

    $tokenResponse_data = json_decode($curl_tokenData);

    $expires = var_dump($tokenResponse_data->{'expires_on'});

    var_dump(date('Y-m-d h:i:s', $expires));

?>

What am I not doing wrong? Any ideas? Thanks.


Solution

  • Looks like a side effect of your use of var_dump that is messing up the $expires variable - this works:

    $expires = 1596726056;
    var_dump($expires);
    $displayValue = date('Y-m-d h:i:s', $expires);
    var_dump($displayValue);