0

I try get user contacts through Outlook contacts REST API. I sucessfully got access token, but when i try to get the contacts i get 404 error.

This is sended url

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

and 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

This is code, which i took right from 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);
      }
    }

Could someone say what i did wrong?

Saril
  • 13
  • 4
  • Can you share the content of the error response? – Jason Johnston Sep 10 '15 at 18:42
  • {"error":{"code":"MailboxNotEnabledForRESTAPI","message":"REST API is not supported for this mailbox."}}. Well... Now i could see the problem by myself. But how could i enable REST API for my mailbox? – Saril Sep 11 '15 at 06:20

1 Answers1

1

The error you're seeing (MailboxNotEnabledForRESTAPI) indicates that your Outlook.com mailbox hasn't been enabled for the APIs yet. Unfortunately there's no setting you can change to enable them yourself. We are enabling mailboxes in batches, so for that particular mailbox you would just need to wait until it gets enabled.

If you'd like to get a test account so you can get a free trial of Office 365, or you can send us an email at outlookdev@microsoft.com to request a developer preview Outlook.com account. For full details, see the Account Requirements section at https://dev.outlook.com/RestGettingStarted.

That is all outdated information now. All Exchange Online and Outlook.com mailboxes are enabled for REST access. For a free developer tenant, sign up for the Microsoft 365 dev program. For docs on the REST API, see the Microsoft Graph docs. The Outlook REST API is deprecated.

Jason Johnston
  • 17,194
  • 2
  • 20
  • 34
  • Any estimate on a timeline for outlook.com mailboxes being enabled for the REST API? I am trying to decide if I need to use the old outlook.com API to get access for a project I am working on. – scramblor Oct 16 '15 at 19:48
  • @Jason Johnson Just a heads up: Your SSL certificate is expired for a few days now at dev.outlook.com. It is also impossible to mail to outlookdev@microsoft.com – Erwin Moller Oct 25 '22 at 11:44
  • That site has been shut down for years :D. We've finally disabled the redirection service on it. You should be going to learn.microsoft.com/outlook now. However, the Outlook REST APIs have been replaced by Microsoft Graph, so a better place to go is learn.microsoft.com/graph. Also no need to email for test accounts anymore, you can get a dev tenant by signing up for the M365 dev program. – Jason Johnston Nov 01 '22 at 15:47