我对所有此类代码都是新手。
要使用 API 获取项目的状态,我必须使用 Bearer 进行身份验证。当它测试时,它给了我令牌,但后来它过期了。
我现在需要尝试获取自己的令牌。我通过第一个 PHP 脚本获得了令牌显示,它告诉我们将令牌放入标头并在脚本中使用令牌。
我尝试了很多方法就是不明白这一点
谢谢
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://console.monogoto.io/Auth');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"UserName\": \"...\",\n \"Password\": \"....\"\n }");
$response = curl_exec($ch);
curl_close($ch);
?>
<?php
$sim = $_POST['sim'];
echo "<br>Sim ID - ";
echo $sim;
echo "<br>";
echo "<br>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://console.monogoto.io/thing/' . $sim . '/state/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'accept: application/json',
'authorization: Bearer {YOUR_TOKEN}',
]);
$response = curl_exec($ch);
print "Status of Sim?: " . $response;
curl_close($ch);
?>
请帮忙
承载令牌,也称为授权令牌,通常具有有限的生命周期,之后必须刷新或重新请求。这是默认工作流程:
如果您还没有令牌,请向授权端点发送请求。 对于具有较长生命周期的令牌,请考虑缓存它们以避免不必要的重新获取。
将令牌包含在 API 请求的授权标头中
授权:不记名{token}
如果令牌过期,请使用提供的refreshToken请求新的令牌,而无需重复身份验证过程。
例如,Monogoto 的 API 在请求令牌时会返回如下响应:
{
"token": "eyJhbGciOiJIUzI1NiIsInR...",
"refreshToken": "eyJhbGciOiJIUzI1NiIsInR..."
}
令牌是包含过期详细信息 (exp) 的 JWT,您可以使用 jwt.io(站点)等工具进行检查。 刷新令牌允许您更新令牌,而无需完全重新身份验证。
代码示例可以如下所示:
<?php
// Function to fetch the authentication token
function getAuthToken($username, $password) {
$url = 'https://console.monogoto.io/Auth'; // Authentication endpoint
$data = json_encode([
"UserName" => $username,
"Password" => $password
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
// Decode the JSON response to extract the token
$responseData = json_decode($response, true);
return $responseData['token'] ?? null;
}
// Function to fetch SIM state using the token
function getSimState($simId, $token) {
$url = 'https://console.monogoto.io/thing/' . $simId . '/state/'; // Endpoint for SIM state
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'accept: application/json',
'authorization: Bearer ' . $token,
]);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Main script logic
$username = 'your_username'; // Replace with your Monogoto username
$password = 'your_password'; // Replace with your Monogoto password
$sim = $_POST['sim'] ?? null; // Retrieve SIM ID from the form submission
if ($sim) {
// Fetch the authorization token
$token = getAuthToken($username, $password);
if ($token) {
echo "<br>Sim ID - " . htmlspecialchars($sim) . "<br><br>";
// Fetch and display SIM status
$simStatus = getSimState($sim, $token);
echo "Status of Sim: " . htmlspecialchars($simStatus);
} else {
echo "Failed to retrieve authentication token.";
}
} else {
echo "SIM ID is missing.";
}
?>