我尝试通过 Outlook 联系人 REST API 获取用户联系人。我成功获得了访问令牌,但是当我尝试获取联系人时,我收到 404 错误。
这是发送的网址
https://outlook.office.com/api/v1.0/me/contacts?%24select=GivenName%2CSurname%2CEmailAddresses&%24orderby=GivenName&%24top=10
和标题
用户代理:php-tutorial/1.0 授权:Bearer----token here----- 接受:应用程序/json 客户端请求 ID:此处为 guid 返回客户端请求 ID:真 X-AnchorMailbox:user_email
公共静态函数 makeApiCall($access_token, $user_email, $method, $url, $payload = NULL)
{
// 生成要始终发送的标头列表。
$标头=数组(
"User-Agent: php-tutorial/1.0", // 发送 User-Agent 标头是最佳实践。
"Authorization: Bearer ".$access_token, // 总是需要我们的授权令牌!
"Accept: application/json", // 始终接受 JSON 响应。
"client-request-id: ".self::makeGuid(), // 用新的 GUID 标记每个新请求。
"return-client-request-id: true", // 告诉服务器在响应中包含我们的请求 ID GUID。
"X-AnchorMailbox: ".$user_email // 提供者用户的电子邮件,用于优化 API 调用的路由
);
$curl = curl_init($url);
开关(strtoupper($方法)){
案例“获取”:
// 无事可做,GET 是默认的,不需要
// 额外的标题。
error_log("正在执行 GET");
休息;
案例“发布”:
error_log("正在做 POST");
// 添加 Content-Type 标头(重要!)
$headers[] = "内容类型:应用程序/json";
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
休息;
案例“补丁”:
error_log("正在打补丁");
// 添加 Content-Type 标头(重要!)
$headers[] = "内容类型:应用程序/json";
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
休息;
案例“删除”:
error_log("正在删除");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
休息;
默认:
error_log("无效方法:".$method);
出口;
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
error_log("curl_exec 完成。");
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
error_log("请求返回状态".$httpCode);
if ($httpCode >= 400) {
返回数组('errorNumber' => $httpCode,
'error' => '请求返回HTTP错误'.$httpCode);
}
$curl_errno = curl_errno($curl);
$curl_err = curl_error($curl);
如果($curl_errno){
$msg = $curl_errno.": ".$curl_err;
error_log("CURL 返回错误:".$msg);
curl_close($curl);
返回数组('errorNumber' => $curl_errno,
'错误' => $msg);
}
别的 {
error_log("响应:".$response);
curl_close($curl);
返回 json_decode($response, true);
}
}
有人可以说我做错了什么吗?