깃허브 로그인 질문합니다.

깃허브 로그인 질문합니다.

QA

깃허브 로그인 질문합니다.

본문

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 은 틀리지 않았습니다.

 


<?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


<?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 에

 


$github_ClientID = 'abcd12341234123412412';
$github_ClientSecret = '0325423hj4h5k23j4h5kj23h45k2345';

 

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

답변을 작성하시기 전에 로그인 해주세요.
전체 123,592 | RSS
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT