AlexanderMaier9458
AlexanderMaier9458

Reputation: 41

Getting MIME-version of message results in "405 Method Not Allowed"-Error

My company runs a web-application that reads out a shared-mailbox and shows the content to the users. It was based on PHP-EWS so far. Since we're moving the mailbox into the Cloud, we have to migrate this application to MS-Graph. I've already migrated the lion's share of the application, but now I encounter a problem when I try to get the MIME-version of a message.

Getting the processed version of the message is no problem at all. The Request for this would be something like:

GET /users/{mailbox-id}/mailfolders/inbox/messages/{message-id}

According to the documentation I have to add "/$value" at the end of the "normal" request to get the mime-version of the message. This works perfectly into Microsoft's Graph-Tester-Website. Within my application I can also request the MIME-version of an ATTACHED message without any problems (works with the same "/$value"-trick). But if I request the MIME-Version of a (normal) NOT-ATTACHED message, I receive the following error:

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `GET https://graph.microsoft.com/v1.0/users/{mailbox-id}/mailfolders/inbox/messages/{message-id}/%24value` 
resulted in a `405 Method Not Allowed` response: { "error": { "code": "ErrorInvalidRequest", "message": "The OData request is not supported.", "innerEr (truncated...) in D:\wwwroot\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 
Stack trace: #0 D:\wwwroot\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) 
#1 D:\wwwroot\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) 
#2 D:\wwwroot\vendor\guzzlehttp\promises\src\Promise.php(156): GuzzleHttp\Promise\Promise::callHand in D:\wwwroot\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113

I used the following code:

$graph = new Graph();
$graph->setAccessToken($tokenCache->getAccessToken());
 /** @var  $contentStream GuzzleHttp\Psr7\Stream */
$getMessageContentUrl = '/users/' . $mailbox . '/mailfolders/inbox/messages/' . $messageId.'/%24value';
try {
    $contentStream = $graph->createRequest('GET', $getMessageContentUrl)
            ->setReturnType("GuzzleHttp\Psr7\Stream")
            ->execute();
} catch (GraphException $e) {
            [...]
}

return $contentStream->getContents();

The same code works if I request the MIME-version of an attached message.

I thought at urlencoding-issues, so I tried "/$value" and "/%24value", same result.

I don't think that it is a permission problem. The OAUTH_SCOPES are

'openid profile offline_access User.Read Mail.Read Mail.Read.Shared Mail.ReadWrite Mail.ReadWrite.Shared Calendars.Read Calendars.Read.Shared'

So I think the account should have more than enough permissions to fulfill this task.

I have no idea, if I am doing something wrong or if it is a problem of the Graph-API or the Graph-PHP-SDK or the Guzzle-Http-Client. I'm using the following versions:

"name": "microsoft/microsoft-graph", "version": "1.11.0",

"name": "guzzlehttp/guzzle", "version": "6.3.3",

"name": "guzzlehttp/psr7", "version": "1.6.1",

I would be very glad if someone could help me or at least set me on the right track.

Upvotes: 2

Views: 685

Answers (2)

AlexanderMaier9458
AlexanderMaier9458

Reputation: 41

I found the solution. Had to replace

GET /users/{mailbox-id}/mailfolders/inbox/messages/{message-id}/$value

by

GET /users/{mailbox-id}/messages/{message-id}/$value

then it works. Kind of strange that other requests with "/mailfolders/inbox/" worked, but this one not. Whatever, as long as it works.

Upvotes: 2

The Method Not Allowed Error means that the route you are requesting exists but not for the method you are using(GET). Try replacing GET method to POST like:

$contentStream = $graph->createRequest('POST', $getMessageContentUrl)
        ->setReturnType("GuzzleHttp\Psr7\Stream")
        ->execute();

Remember api's routes specifys methods alowed(GET, POST, PUT, PATCH, DELETE);

Upvotes: 0

Related Questions