/** * Retrieve user emails using the Email API's Messages * @param string $user user email address * @param int $count number of emails to request * @param string $order order in which to query emails (desc or asc) * @return array Messages resposnse */ public function retrieve($user, $count = '10', $order = 'desc') { $accessToken = $_SESSION['access_token']; // Build the API request paramaters $queryParams = '?$select=From,Subject,DateTimeReceived&$orderby=DateTimeReceived%20' . $order . '&$top=' . $count; // Build the API Base Url $url = Office365::$resourceBaseUrl . 'api/v' . Office365::$apiVersion . '/users/' . $user . '/Messages/'; // Build the header object $headers = array('Authorization: Bearer ' . $accessToken, 'Content-Type: application/json', 'Accept:application/json'); // generate a new API request and return the response as an array $request = new HttpPost($url . $queryParams); $request->setPostHeaders($headers); $request->send(); $responseObj = json_decode($request->getHttpResponse()); return $responseObj; }
/** * Retrieve Access Token * @return array response from access token request */ public function retrieve() { // parse token and get the tenant id. array key tid in response $parsedToken = $this->parse(); $tenantId = $parsedToken['tid']; if ($tenantId) { // if we have a tenant id built the token url and generate the assertion $this->tokenUrl = $this->authorizationBaseUrl . '/' . $tenantId . '/oauth2/token'; $assertion = new Assertion(); $getAssertion = $assertion->get($this->tokenUrl); //build the post data array $queryParams = array('resource' => $this->resource, 'client_id' => Office365::getClientId(), 'client_assertion_type' => 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer', 'client_assertion' => $getAssertion, 'grant_type' => 'client_credentials', 'redirect_uri' => $this->redirectUri); //generate a new API request using the tokenUrl and post_form array $request = new HttpPost($this->tokenUrl); $request->setPostData($queryParams); $request->send(); $responseObj = json_decode($request->getHttpResponse()); return $responseObj; } }
/** * Retrieve 30 days of upcoming calendar events using the Calendar API's CalendarView * @param string $user user email address * @return array CalendarView response */ public function retrieve($user) { $accessToken = $_SESSION['access_token']; // Set the start of our view window to midnight of today. $date = new \DateTime('now'); $start = $date->setTime(0, 0, 0); $startUrl = self::encodeDateTime($start); // Add 30 days to the start date to get the end date. $end = $start->add(new \DateInterval("P30D")); $endUrl = self::encodeDateTime($end); // Build the API request paramaters $queryParams = "?" . "startDateTime=" . $startUrl . "&endDateTime=" . $endUrl . "&\$select=Subject,Start,End" . "&\$orderby=Start"; // Build the API Base Url $url = Office365::$resourceBaseUrl . 'api/v' . Office365::$apiVersion . '/users/' . $user . '/CalendarView/'; // Build the header object $headers = array('Authorization: Bearer ' . $accessToken, 'Content-Type: application/json', 'Accept:application/json'); // generate a new API request and return the response as an array $request = new HttpPost($url . $queryParams); $request->setPostHeaders($headers); $request->send(); $responseObj = json_decode($request->getHttpResponse()); return $responseObj; }
<?php /** * learning-study에서 사용하는 redirect.. 파일 이동 필요합니다. :) * redirect page로 forwading 될 때, 인증 코드(authorization code)를 가져온 후 * OAuth2 서버에 다시 요청을 하여 Access Token을 받습니다. */ require 'learning-study/config.php'; require 'learning-study/HttpPost.class.php'; if (isset($_GET['code'])) { $code = $_GET['code']; $url = 'https://login.live.com/oauth20_token.srf'; $params = ['code' => $code, 'client_id' => $oauth2_client_id, 'client_secret' => $oauth2_secret, 'redirect_uri' => $oauth2_redirect, 'grant_type' => 'authorization_code']; // header('Content-Type: application/x-www-form-urlencoded'); // 예제에 나온 것이지만 이것을 설정하면 파일로 다운로드가 된다. $request = new HttpPost($url); $request->setPostData($params); $request->send(); $responseObj = json_decode($request->getHttpResponse()); var_dump($responseObj); // echo 'OAuth2 server provided access token: ' . $responseObj->access_token; }