【发布时间】:2021-04-10 00:00:04
【问题描述】:
我用过这个 api
curl -X POST
https://tracking.api.here.com/v2/token
-H '授权:承载 {signedRequest}'
我的php代码是
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://tracking.api.here.com/v2/timestamp');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
$result = json_decode($result);
curl_close($ch);
$timestamp = ($result->timestamp);
$accessKey = $deviceId;
$secret = $deviceSecret;
$url = "https://account.api.here.com/oauth2/token";
$nonce = mt_rand(100000,999999);
$timestamp = $timestamp;
$baseString = "grant_type=client_credentials&oauth_consumer_key=".$accessKey."&oauth_nonce=".$nonce."&oauth_signature_method=HMAC-SHA256&oauth_timestamp=".$timestamp."&oauth_version=1.0";
$workingString = 数组();
foreach (explode('&', $baseString) as $parameter) {
$parameter = explode('=', $parameter);
$workingString[] = urlencode($parameter[0]).'='.trim(urlencode($parameter[1]));
}
$urlEncodeParamaterString = implode('&', $workingString);
$fullBaseString = "POST&".urlencode($url)."&".urlencode($urlEncodeParamaterString); $hashKey = $secret.'&';
函数编码($data) {
return str_replace(['+', '/'], ['-', '_'], base64_encode($data));
}
函数解码($data) {
return base64_decode(str_replace(['-', '_'], ['+', '/'], $data));
}
$binaryKey = 解码($hashKey);
$signature = encode(hash_hmac("sha256", $fullBaseString, $binaryKey, true));
$authHeader = "OAuth oauth_consumer_key=".$accessKey.",oauth_signature_method='HMAC-SHA256',oauth_timestamp='".$timestamp."',oauth_nonce=".$nonce.",oauth_version='1.0' ,oauth_signature=".$signature."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://tracking.api.here.com/v2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = 数组();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = '授权:'.$authHeader;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
回声''; print_r($result);
输出
{
代码:401,
id: "312f56e5-db4f-40f5-807f-c5a0413dd668",
消息:“未找到给定 deviceId 的许可证。”,
error: "Timestamp wrong 当请求时间戳与服务器时间相差超过 10 秒时,将带有当前服务器时间戳的 x-here-timestamp 标头添加到响应中。不正确的签名 如果 OAuth 签名不正确,则响应将是 401 但没有 x-here-timestamp 字段。"
}
【问题讨论】: