Errors With API
Hi there, I'm looking to use the Graph API with PHP, however I'm running into a few errors. When I first set up my account and run the oauth loop, it works properly. I'm directed to the /sendmail.php. When I click to send a mail message as myself, without modifying the code, I get an error back:
array(1) { ["error"]=> array(3) { ["code"]=> string(10) "BadRequest" ["message"]=> string(114) "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format." ["innerError"]=> array(2) { ["request-id"]=> string(36) "1dfc3c80-674e-4234-a62a-92a45b519a4c" ["date"]=> string(19) "2016-12-12T16:52:31" } } } array(26) { ["url"]=> string(44) "https://graph.microsoft.com/v1.0/me/sendmail" ["content_type"]=> string(16) "application/json" ["http_code"]=> int(400) ["header_size"]=> int(495) ["request_size"]=> int(1331) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.365301) ["namelookup_time"]=> float(0.072895) ["connect_time"]=> float(0.104058) ["pretransfer_time"]=> float(0.288482) ["size_upload"]=> float(2579) ["size_download"]=> float(310) ["speed_download"]=> float(848) ["speed_upload"]=> float(7059) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(2579) ["starttransfer_time"]=> float(0.322992) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(13) "157.56.28.135" ["certinfo"]=> array(0) { } ["primary_port"]=> int(443) ["local_ip"]=> string(15) "192.168.231.202" ["local_port"]=> int(57747) }
I've dumped out the curl opts / headers and they all look correct. So, moving on.
I attempted to use Guzzle to make a request with the existing oauth token to see if I could list users or something, so I tried this:
<?php
require_once('../autoload.php');
require_once __DIR__ . '/../vendor/autoload.php';
use Microsoft\Graph\Connect\Constants;
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$client = new GuzzleHttp\Client;
try {
$response = $client->request('GET', Constants::RESOURCE_ID . Constants::USERS_ENDPOINT, [
'headers' => [
'Authorization' => 'Bearer ' . $_SESSION['access_token'],
'Content-Type' => 'application/json'
]
]);
} catch (Exception $e) {
var_dump(json_decode($e->getResponse()->getBody()->getContents()));
}
var_dump($response->getBody());
die();
Which gives me an error:
["error"]=> object(stdClass)#30 (3) { ["code"]=> string(12) "UnknownError" ["message"]=> string(122) "{"Status":500,"Message":"All the offeractions povided in the property bag cannot be validated for the token.\u000d\u000a"}" ["innerError"]=> object(stdClass)#17 (2) { ["request-id"]=> string(36) "bff1baa2-db7c-412c-99a4-42f5f9b3bfb4" ["date"]=> string(19) "2016-12-12T16:54:57" } }
Obviously this is an internal error on the Microsoft API, however I have no clue how to interpret that or handle it. It's not even spelled properly either. Just wondering what I can do to get this simple example working?
I've started looking at this, and I've managed to reproduce your first problem, but only in IE. I can successfully send the email from Edge and Chrome. I have no idea why it would break for me in only IE, but could you confirm that 1.) you're seeing the problem in IE, and 2. (if possible) whether you see the problem in any browser other than IE?
As for your second issue, your token will have permissions for only these scopes (the ones expressly requested in the consent screen):
View your basic profile
Sign in as you
Send mail as you
These correspond to the scopes defined in Constants.php: 'openid profile mail.send'
If you were trying to get a list of users you'd need a token that had the User.ReadBasic.All permission. The error you're seeing is not helpful, but I'm not surprised that the request didn't work, and I'm pretty sure that the missing permission is the root of the problem. You could test this assumption by adding the User.ReadBasic.All permission to the Constants.php file, going through the authentication and consent process via the sample, and retrying your Guzzle request with the fresh token.
I'll continue investigating the error you're seeing when you try to send mail.
Could you try again with the small update that I just checked in? The update to the Constants.php file adds one additional scope to the authorization: user.read. When I make this change, the problem with /sendmail disappears for me in IE.
Also, my guess is that if you added the user.readbasic.all permission to your access token, you'd see your Guzzle request to the /users endpoint start to work.
Closing for now. Please reopen if this problem persists after the change.