<?php
$tokenrf=file_get_contents('rf');
$endpoint = 'https://star.api.edge.bamgrid.com/graph/v1/device/graphql';
$headers = [
    'Host: star.api.edge.bamgrid.com',
    'Accept: application/json',
    'Authorization: c3RhciZhbmRyb2lkJjEuMC4w.cNMxdgOjN9t9gJ-h-8KIAO7h_PnjWs2BWWh56Zhj5zc',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'Content-Type: application/json',
];

$data = '{"query":"mutation refreshToken($input: RefreshTokenInput!) {\n            refreshToken(refreshToken: $input) {\n                activeSession {\n                    sessionId\n                }\n            }\n        }","variables":{"input":{"refreshToken":"'.$tokenrf.'"}},"operationName":"refreshToken"}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$response = curl_exec($ch);


if ($response === false) {
    echo "Falha na solicitação: " . curl_error($ch);
} else {
    //print_r(json_decode($response, true));
    $tk= (json_decode($response, true)['extensions']['sdk']['token']['accessToken']);
    $rf= (json_decode($response, true)['extensions']['sdk']['token']['refreshToken']);
}
echo "$tk";
$file = fopen('tk', 'w');
fwrite($file, $tk);
fclose($file);
$file2 = fopen('rf', 'w');
fwrite($file2, $rf);
fclose($file2);
curl_close($ch);
?>
	
