/** * 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; } }
<?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; }
*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", "expires_in": 3092,