<?php // Step 1: 사용자는 승인으로 리디렉션됩니다URL $authorizationUrl = 'https://example.com/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&response_type=code'; header('Location: ' . $authorizationUrl); exit; // Step 2: 인증 서버는 사용자를 콜백으로 리디렉션합니다URL,인증 코드를 제공합니다 $authorizationCode = $_GET['code']; // Step 3: 승인 코드를 사용하여 액세스 토큰을 얻으십시오 $tokenUrl = 'https://example.com/oauth/token'; $tokenData = [ 'grant_type' => '인증 _code', 'code'=> $ emustizationCode, 'redirect_uri'=> 'your_redirect_uri', 'client_id'=> 'your_client_id', 'client_secret'=> 'your_client_secret', ]; $ tokenoptions = [ 'http'=> [ 'method'=> 'post', '헤더'=> '컨텐츠 유형 : Application/x-www-form-urlencoded', 'content'=> http_build_query ($ tokendata) ]] ]; $ tokencontext = stream_context_create ($ tokenoptions); $ tokenresult = file_get_contents ($ tokenurl, false, $ tokencontext); $ accesstoken = json_decode ($ tokenresult)-> access_token; // 4 단계 : Access Token을 사용하여 사용자 리소스를 얻습니다. $ userurl = 'https://example.com/api/user'; $ useroptions = [ 'http'=> [ 'method'=> 'get', '헤더'=> '승인 : bearer'. $ accesstoken ]] ]; $ USERCONTEXT = Stream_Context_Create ($ userOptions); $ USERRESULT = FILE_GET_CONTENTS ($ USERUL, FALSE, $ USERCONTEXT); $ userData = JSON_DECODE ($ USERRESULT); // 프로세스 사용자 데이터 Echo 'Welcome'. $ userData-> 이름; ?>
관련 태그:
API