깃허브 로그인 질문합니다. 채택완료
akworjs0517
5년 전
조회 3,449
https://docs.github.com/en/developers/apps/authorizing-oauth-apps#web-application-flow
이 문서를 읽으면서 아래 소스코드를 작성했습니다.
그리고 아래의 url 로 접속하여 Access 권한을 줫을때 코드는 잘받아와집니다.
하지만 토큰 발급으로가면 NOT FOUND 가 반환되는데 어디가 문제인건가요..?
CLIENT_ID, CLIENT_SECRET 은 틀리지 않았습니다.
</p>
<p><?php</p>
<p> </p>
<p>define('OAUTH2_CLIENT_ID', '');</p>
<p>define('OAUTH2_CLIENT_SECRET', '');</p>
<p>define('AUTHORIZE_URL', '<a href="https://github.com/login/oauth/authorize');" target="_blank" rel="noopener noreferrer">https://github.com/login/oauth/authorize');</a></p>
<p>define('TOKEN_URL', '<a href="https://github.com/login/oauth/access_token');" target="_blank" rel="noopener noreferrer">https://github.com/login/oauth/access_token');</a></p>
<p>define('API_URL', '<a href="https://api.github.com/');" target="_blank" rel="noopener noreferrer">https://api.github.com/');</a></p>
<p> </p>
<p>$data = array(</p>
<p> 'client_id' => OAUTH2_CLIENT_ID,</p>
<p> 'client_secret' => OAUTH2_CLIENT_SECRET,</p>
<p> 'code' => $_GET['code']</p>
<p>);</p>
<p> </p>
<p>$headers[] = 'Accept: application/json';</p>
<p>$url = "<a href="https://github.com/login/oauth/access_token";" target="_blank" rel="noopener noreferrer">https://github.com/login/oauth/access_token";</a></p>
<p> </p>
<p>$ch = curl_init(); //curl 초기화</p>
<p>curl_setopt($ch, CURLOPT_URL, $url); //URL 지정하기</p>
<p>curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //요청 결과를 문자열로 반환 </p>
<p>curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //connection timeout 10초 </p>
<p>curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //원격 서버의 인증서가 유효한지 검사 안함</p>
<p>curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //POST data</p>
<p>curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //Headers</p>
<p>curl_setopt($ch, CURLOPT_POST, true); //true시 post 전송 </p>
<p> </p>
<p>$response = curl_exec($ch);</p>
<p>curl_close($ch);</p>
<p>
</p>
<p>print_r($response);</p>
<p>?></p>
<p>
댓글을 작성하려면 로그인이 필요합니다.
답변 1개
채택된 답변
+20 포인트
5년 전
</p>
<p><?php</p>
<p>/*</p>
<p> * login_with_github.php</p>
<p> *</p>
<p> * @(#) $Id: login_with_github.php,v 1.2 2014/03/16 11:23:53 mlemos Exp $</p>
<p> *</p>
<p> */</p>
<p> </p>
<p> /*</p>
<p> * Get the http.php file from <a href="http://www.phpclasses.org/httpclient" target="_blank" rel="noopener noreferrer">http://www.phpclasses.org/httpclient</a></p>
<p> */</p>
<p> include_once('./_common.php');</p>
<p> require('http.php');</p>
<p> require('oauth_client.php');</p>
<p> </p>
<p> $client = new oauth_client_class;</p>
<p> $client->debug = false;</p>
<p> $client->debug_http = true;</p>
<p> $client->server = 'github';</p>
<p> $client->redirect_uri = '<a href="https://'.$_SERVER['HTTP_HOST']." target="_blank" rel="noopener noreferrer">https://'.$_SERVER['HTTP_HOST'].</a></p>
<p> dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/login_with_github.php';</p>
<p> </p>
<p> $client->client_id = $github_ClientID; $application_line = __LINE__;</p>
<p> $client->client_secret = $github_ClientSecret;</p>
<p> </p>
<p> if(strlen($client->client_id) == 0</p>
<p> || strlen($client->client_secret) == 0)</p>
<p> alert_close('Github 연동키값을 확인해 주세요.');</p>
<p> /*</p>
<p> die('Please go to github applications page '.</p>
<p> '<a href="https://github.com/settings/applications/new in the API access tab, '." target="_blank" rel="noopener noreferrer">https://github.com/settings/applications/new in the API access tab, '.</a></p>
<p> 'create a new client ID, and in the line '.$application_line.</p>
<p> ' set the client_id to Client ID and client_secret with Client Secret. '.</p>
<p> 'The Callback URL must be '.$client->redirect_uri);</p>
<p> */</p>
<p> /* API permissions</p>
<p> */</p>
<p> $client->scope = 'user:email';</p>
<p> if(($success = $client->Initialize()))</p>
<p> {</p>
<p> if(($success = $client->Process()))</p>
<p> {</p>
<p> if(strlen($client->authorization_error))</p>
<p> {</p>
<p> $client->error = $client->authorization_error;</p>
<p> $success = false;</p>
<p> }</p>
<p> elseif(strlen($client->access_token))</p>
<p> {</p>
<p> $success = $client->CallAPI(</p>
<p> '<a href="https://api.github.com/user'," target="_blank" rel="noopener noreferrer">https://api.github.com/user',</a></p>
<p> 'GET', array(), array('FailOnAccessError'=>true), $user);</p>
<p> }</p>
<p> }</p>
<p> $success = $client->Finalize($success);</p>
<p> }</p>
<p> if($client->exit)</p>
<p> exit;</p>
<p> if($success)</p>
<p> {</p>
<p> $client->GetAccessToken($AccessToken);</p>
<p> </p>
<p> $mb_gubun = 'git_';</p>
<p> $mb_id = $user->login;</p>
<p> $mb_name = $user->login;</p>
<p> $mb_nick = $user->login;</p>
<p> $mb_email = $user->emails;</p>
<p> $token_value = $AccessToken['value'];</p>
<p> $token_secret = '';</p>
<p> $token_refresh = $AccessToken['refresh'];</p>
<p> </p>
<p> //$client->ResetAccessToken();</p>
<p> </p>
<p> include_once('./oauth_check.php');</p>
<p> } else {</p>
<p> $error = HtmlSpecialChars($client->error);</p>
<p> alert_close($error);</p>
<p> }</p>
<p>?></p>
<p>
예전에 login-oauth로 이렇게 구성해서 썼었습니다.
도움이 되실지 모르겠네요.
_apikey.php 에
</p>
<p>$github_ClientID = 'abcd12341234123412412';</p>
<p>$github_ClientSecret = '0325423hj4h5k23j4h5kj23h45k2345';</p>
<p>
이렇게 넣어줬구요. 최신까지 사용했었습니다.
로그인 후 평가할 수 있습니다
답변에 대한 댓글 2개
a
akworjs0517
5년 전
scope 는 필수가 아니던데 입력하지않으면 안되나요?
�
우성짱
5년 전
되는 것 확인만 해서...
구체적인 부분은 잘 모르겠습니다.
구체적인 부분은 잘 모르겠습니다.
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인