Php 카카오 로그인 api

function get_access_token($code)
{
    $data = array(
        'grant_type'   => 'authorization_code',
        'client_id'    => $this->client_id,
        'redirect_uri' => $this->redirect_uri,
        'code'         => $code
    );
    $post_data = http_build_query($data);
    // $this->$post_data_val = $post_data;


    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $this->token_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    $json = curl_exec($ch);
    curl_close($ch);
    // echo $json;
    if($json) {
        $result = json_decode($json);

        // echo "token:".$result->access_token;

        $this->access_token  = $result->access_token;
        $this->refresh_token = $result->refresh_token;
       
        return true;
    } else {
        return false;
    }
}

function get_profile()
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->profile_url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$this->access_token));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    // curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    // curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
    // curl_setopt($ch, CURLOPT_TIMEOUT, 5);


    $json = curl_exec($ch);

    var_dump($json);
    curl_close($ch);

    if($json)
        $this->profile = json_decode($json);
}

그누보드에서 카카오 로그인데이터 를 가져오려고 하니까
하단 var_dump($json)부분에서
{“msg”:“Not Found”,“code”:-404}
가 뜨네요ㅜ

토큰값은 잘 가져오는 것 같은데
get_profile 함수에서 자꾸 에러가 납니다 ㅜㅜ

@teenanda

profile_url 의 요청주소가 잘못된 것이 아닌지 확인해 보시는 것이 좋을 것 같아요.

https://developers.kakao.com/docs/latest/ko/kakaologin/rest-api#req-user-info

1개의 좋아요

으어…
기존에 쓰던 요청 주소가 https://kapi.kakao.com/v1/user/me 이였네요… 한참 삽질 했습니다 ㅜㅜ
https://kapi.kakao.com/v2/user/me 일줄이야…

덕분에 잘 해결했습니다 감사합니다 ㅎ

1개의 좋아요