Search code examples
office365api

Office365 API returns: the hostname component of the audience claim value 'https://graph.microsoft.com' is invalid


I can't seem to work this out. I found some questions close to this issue that says configurations is the issue, however, I am able to use the send mail function. I am just starting to learn Office365 API and would greatly appreciate the help!

Here is the snippet:

$curl = curl_init(
$headers = array(
    'Authorization: Bearer ' . $_SESSION['access_token'],
    'Content-Type: application/json;' .
    'odata.metadata=minimal;' .
    'odata.streaming=true'
);

curl_setopt_array(
    $curl,
    array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'https://outlook.office.com/api/v2.0/me/MailFolders/inbox/messages/',
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_VERBOSE => 1,
        CURLOPT_HEADER => 1
    )
);

// The following curl options can be used in development to debug the code.
// Option to disable certificate verification. Do not use on production env.
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// Option to set a proxy for curl to use.
// Useful if you want to review traffic with a tool like Fiddler.
// curl_setopt($curl, CURLOPT_PROXY, '127.0.0.1:8888');

// Enable error reporting on curl
//curl_setopt($curl, CURLOPT_FAILONERROR, true);

// Send the request & save response to a variable
$response = curl_exec($curl);
// Check for errors
if (curl_errno($curl)) {
    print_r(curl_error($curl));
    throw new \RuntimeException(curl_error($curl));
}

echo '<pre>';
print_r($response);

// Close request and clear some resources
curl_close($curl);

Returns:

x-ms-diagnostics: 2000003;reason="The hostname component of the audience claim value 'https://graph.microsoft.com' is invalid";error_category="invalid_resource"

If it is the configurations (Azure AD), then what do I need to check? And if this helps, I've granted all permissions to Microsoft Graph in Azure AD.


Solution

  • Your access token you've specified is for https://graph.microsoft.com while you are trying to access https://outlook.office.com.

    Try https://graph.microsoft.com/v1.0/me/messages instead of https://outlook.office.com/api/v2.0/me/MailFolders/inbox/messages.