/** * 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; } }
/** * Chama uma servlet e devolve uma string * @param string $url * @param string $post string para postar Ex.: var1=val1&var2=val2 * @return string * @throws Exception */ public function getAsString($url, HttpPost $post = null) { $ch = curl_init($this->makeUrl($url)); if (!empty($post)) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post->get()); } curl_setopt($ch, CURLOPT_MUTE, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeOut); curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeOut); $result = curl_exec($ch); if (curl_errno($ch)) { throw new Exception(curl_error($ch), curl_errno($ch)); } curl_close($ch); return $result; }
/** * 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; }
*Nos aseguramos que no hay falsificación de petición , y que el usuario *que envia esta solicitud de conexión es el usuario que se suponía. */ echo "Este es el estado autogenerado" . $_SESSION['state']; echo "<br>"; echo "Este es el estado segun google" . $stateRequested; echo "<br>"; if ($_SESSION['state'] != $stateRequested) { die('Invalid state parameter'); } $url = "https://accounts.google.com/o/oauth2/token"; $post = array("code" => $code, "client_id" => $oauth2_client_id, "client_secret" => $oauth2_secret, "redirect_uri" => $oauth2_redirect, "grant_type" => "authorization_code"); //convertimos $post en un post String que usaremos en nuestro HttpPost. $postText = http_build_query($post); //creamos un objeto HttpPost pasando por parametro la URL a la que vamos a acceder. $request = new HttpPost($url); $request->setPostData($postText); $request->send(); //decodificamos el string con formato json $data = json_decode($request->getResponse()); //Alamacenamos los tokens $id_token = $data->id_token; $access_token = $data->access_token; //Url que nos da la informacion del usuario en formato JWT (json web token). $url_id_token = "https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=" . $id_token; /* la informacion es de esta forma: { "issuer": "accounts.google.com", "issued_to": "547638711794-hn5b8ikbbhvaqodjeh6v36hcm7i8uk94.apps.googleusercontent.com", "audience": "547638711794-hn5b8ikbbhvaqodjeh6v36hcm7i8uk94.apps.googleusercontent.com", "user_id": "118279672935836979189",