I am trying to authenticate with the new Office 365 API. I have successfully authenticated and I'm swimming in a garden of roses.
However, the last part is to sent a custom HTTP request with my shiny new access token. It doesn't seem to be working though.
Can someone please outline how to send the http request detailed below with PHP?
GET https://outlook.office365.com/EWS/OData/Me/Inbox/Messages?$top=5
User-Agent: My-Cool-WebApp/1.0
client-request-id: 9aa1c740-25e2-4841-9146-ef7018f7bf37
return-client-request-id: true
authorization: Bearer {access token your app received in Step Three}
Thanks
Code Below:
$code = $_GET['code'];
$accessToken = AccessCode::getCode($code);
$EmailData = EmailSearch::getEmail($accessToken);
class AccessCode
{
public static function getCode($code)
{
//
$url = 'https://login.windows.net/common/oauth2/token';
//
$data = array('grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => 'redirect_uri','client_id' => 'client_id','client_secret' => 'client_secret', 'resource' => '00000002-0000-0ff1-ce00-000000000000');
//
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
//
$context = stream_context_create($options);
//
$result = json_decode(file_get_contents($url, false, $context));
//
return $result->access_token;
}
}
class EmailSearch
{
public static function getEmail($accessToken)
{
//
$url = 'https://outlook.office365.com/EWS/OData/Me';
$requestID = create_guid();
$request_headers = array('client-request-id: '. $requestID,
'return-client-request-id: true',
"Authorization: Bearer $accessToken");
//
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'GV-Email-Forward/1.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($result);
}
}
function create_guid($namespace = '') {
static $guid = '';
$uid = uniqid("", true);
$data = $namespace;
$data .= $_SERVER['REQUEST_TIME'];
$data .= $_SERVER['HTTP_USER_AGENT'];
$data .= $_SERVER['LOCAL_ADDR'];
$data .= $_SERVER['LOCAL_PORT'];
$data .= $_SERVER['REMOTE_ADDR'];
$data .= $_SERVER['REMOTE_PORT'];
$hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
$guid = '{' .
substr($hash, 0, 8) .
'-' .
substr($hash, 8, 4) .
'-' .
substr($hash, 12, 4) .
'-' .
substr($hash, 16, 4) .
'-' .
substr($hash, 20, 12) .
'}';
return $guid;
}