깃허브 로그인 질문합니다. 채택완료

https://docs.github.com/en/developers/apps/authorizing-oauth-apps#web-application-flow

이 문서를 읽으면서 아래 소스코드를 작성했습니다.

그리고 아래의 url 로 접속하여 Access 권한을 줫을때 코드는 잘받아와집니다.

https://github.com/login/oauth/authorize?client_id=CLIENT_ID&redirect_uri=http://localhost/callback.php

 

하지만 토큰 발급으로가면 NOT FOUND 가 반환되는데 어디가 문제인건가요..?

CLIENT_ID, CLIENT_SECRET 은 틀리지 않았습니다.

 

Copy
<?php

 

define('OAUTH2_CLIENT_ID', '');

define('OAUTH2_CLIENT_SECRET', '');

define('AUTHORIZE_URL', 'https://github.com/login/oauth/authorize');

define('TOKEN_URL', 'https://github.com/login/oauth/access_token');

define('API_URL', 'https://api.github.com/');

 

$data = array(

    'client_id' => OAUTH2_CLIENT_ID,

    'client_secret' => OAUTH2_CLIENT_SECRET,

    'code' => $_GET['code']

);

 

$headers[] = 'Accept: application/json';

$url = "https://github.com/login/oauth/access_token";

 

$ch = curl_init();                                 //curl 초기화

curl_setopt($ch, CURLOPT_URL, $url);               //URL 지정하기

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    //요청 결과를 문자열로 반환 

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);      //connection timeout 10초 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   //원격 서버의 인증서가 유효한지 검사 안함

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);       //POST data

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    //Headers

curl_setopt($ch, CURLOPT_POST, true);              //true시 post 전송 

 

$response = curl_exec($ch);

curl_close($ch);


 

print_r($response);

?>

답변 1개

채택된 답변
+20 포인트

Copy
<?php

/*

 * login_with_github.php

 *

 * @(#) $Id: login_with_github.php,v 1.2 2014/03/16 11:23:53 mlemos Exp $

 *

 */

 

    /*

     *  Get the http.php file from http://www.phpclasses.org/httpclient

     */

     include_once('./_common.php');

    require('http.php');

    require('oauth_client.php');

 

    $client = new oauth_client_class;

    $client->debug = false;

    $client->debug_http = true;

    $client->server = 'github';

    $client->redirect_uri = 'https://'.$_SERVER['HTTP_HOST'].

        dirname(strtok($_SERVER['REQUEST_URI'],'?')).'/login_with_github.php';

 

    $client->client_id = $github_ClientID; $application_line = __LINE__;

    $client->client_secret = $github_ClientSecret;

 

    if(strlen($client->client_id) == 0

    || strlen($client->client_secret) == 0)

        alert_close('Github 연동키값을 확인해 주세요.');

    /*

        die('Please go to github applications page '.

            'https://github.com/settings/applications/new in the API access tab, '.

            'create a new client ID, and in the line '.$application_line.

            ' set the client_id to Client ID and client_secret with Client Secret. '.

            'The Callback URL must be '.$client->redirect_uri);

    */

    /* API permissions

     */

    $client->scope = 'user:email';

    if(($success = $client->Initialize()))

    {

        if(($success = $client->Process()))

        {

            if(strlen($client->authorization_error))

            {

                $client->error = $client->authorization_error;

                $success = false;

            }

            elseif(strlen($client->access_token))

            {

                $success = $client->CallAPI(

                    'https://api.github.com/user',

                    'GET', array(), array('FailOnAccessError'=>true), $user);

            }

        }

        $success = $client->Finalize($success);

    }

    if($client->exit)

        exit;

    if($success)

    {

        $client->GetAccessToken($AccessToken);

 

        $mb_gubun = 'git_';

        $mb_id = $user->login;

        $mb_name = $user->login;

        $mb_nick = $user->login;

        $mb_email = $user->emails;

        $token_value = $AccessToken['value'];

        $token_secret = '';

        $token_refresh = $AccessToken['refresh'];

 

        //$client->ResetAccessToken();

 

        include_once('./oauth_check.php');

    } else {

        $error = HtmlSpecialChars($client->error);

        alert_close($error);

    }

?>

 

예전에 login-oauth로 이렇게 구성해서 썼었습니다.

 

도움이 되실지 모르겠네요.

 

_apikey.php 에

 

Copy
$github_ClientID = 'abcd12341234123412412';

$github_ClientSecret = '0325423hj4h5k23j4h5kj23h45k2345';

 

이렇게 넣어줬구요. 최신까지 사용했었습니다.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

scope 는 필수가 아니던데 입력하지않으면 안되나요?
되는 것 확인만 해서...

구체적인 부분은 잘 모르겠습니다.

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고