Microsoft outlook API 出现 404 错误

Microsoft outlook API give 404 error

我尝试通过 Outlook 联系人 REST API 获取用户联系人。我成功获得了访问令牌,但是当我尝试获取联系人时出现 404 错误。

已发送url

https://outlook.office.com/api/v1.0/me/contacts?%24select=GivenName%2CSurname%2CEmailAddresses&%24orderby=GivenName&%24top=10

和headers

 User-Agent: php-tutorial/1.0
 Authorization: Bearer ----token here-----
 Accept: application/json
 client-request-id: guid here
 return-client-request-id: true
 X-AnchorMailbox: user_email

这是我从 Microsoft tutorial

中获取的代码
    public static function makeApiCall($access_token, $user_email, $method, $url, $payload = NULL)
    {
      // Generate the list of headers to always send.
      $headers = array(
        "User-Agent: php-tutorial/1.0",         // Sending a User-Agent header is a best practice.
        "Authorization: Bearer ".$access_token, // Always need our auth token!
        "Accept: application/json",             // Always accept JSON response.
        "client-request-id: ".self::makeGuid(), // Stamp each new request with a new GUID.
        "return-client-request-id: true",       // Tell the server to include our request-id GUID in the response.
        "X-AnchorMailbox: ".$user_email         // Provider user's email to optimize routing of API call
      );

      $curl = curl_init($url);

      switch(strtoupper($method)) {
        case "GET":
          // Nothing to do, GET is the default and needs no
          // extra headers.
          error_log("Doing GET");
          break;
        case "POST":
          error_log("Doing POST");
          // Add a Content-Type header (IMPORTANT!)
          $headers[] = "Content-Type: application/json";
          curl_setopt($curl, CURLOPT_POST, true);
          curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
          break;
        case "PATCH":
          error_log("Doing PATCH");
          // Add a Content-Type header (IMPORTANT!)
          $headers[] = "Content-Type: application/json";
          curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
          curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
          break;
        case "DELETE":
          error_log("Doing DELETE");
          curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
          break;
        default:
          error_log("INVALID METHOD: ".$method);
          exit;
      }

      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
      $response = curl_exec($curl);
      error_log("curl_exec done.");

      $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
      error_log("Request returned status ".$httpCode);
      if ($httpCode >= 400) {
        return array('errorNumber' => $httpCode,
                     'error' => 'Request returned HTTP error '.$httpCode);
      }

      $curl_errno = curl_errno($curl);
      $curl_err = curl_error($curl);
      if ($curl_errno) {
        $msg = $curl_errno.": ".$curl_err;
        error_log("CURL returned an error: ".$msg);
        curl_close($curl);
        return array('errorNumber' => $curl_errno,
                     'error' => $msg);
      }
      else {
        error_log("Response: ".$response);
        curl_close($curl);
        return json_decode($response, true);
      }
    }

谁能告诉我哪里做错了?

您看到的错误 (MailboxNotEnabledForRESTAPI) 表示您的 Outlook.com 邮箱尚未启用 API。不幸的是,您无法更改设置以自行启用它们。我们正在分批启用邮箱,因此对于特定的邮箱,您只需等待它被启用。

如果您想获得一个测试帐户以便免费试用 Office 365,或者您可以向我们发送电子邮件至 outlookdev@microsoft.com 以请求开发人员预览 Outlook.com 帐户。有关完整详细信息,请参阅 https://dev.outlook.com/RestGettingStarted 中的 帐户要求 部分。