모바일 웹에서 카카오스토리 글 올리기
페이지 정보
작성자
본문
지난번에 카카오 앱개발을 위한 준비과정에 대해서 이야기해 봤습니다.
오늘은 카카오 api를 사용해서 스토리에 글쓰기를 해보겠습니다.
api를 사용하기위해서는 먼저 카카오로 앱개발하기로 앱을 만들셔야 합니다.
아직 카카오 앱을 만들지 않았다면,
카카오 앱개발 시작하기를 참고해주세요
스토리에 글쓰기를 하기위해서는 먼저 카카오계정으로 로그인을 해야합니다.
이 기능은 다른 포털에서 제공하는 로그인기능과 같다고 보시면됩니다.
api를 사용하기위해서 access_token이라는 일종의 방문증을 받는셈이죠.
https://developers.kakao.com/buttons
여기서 카카오 로그인버튼을 다운로드 받으시고 로그인 페이지를 만들어주세요.
https://kauth.kakao.com/oauth/authorize?client_id=".CACAO_APPID."&redirect_uri=".CACAO_REDIRECT."&response_type=code
버튼에 링크는 이렇게 설정하면됩니다.
카카오계정을 입력하고 동의 버튼 클릭~
그러면 카카오서버에서 코드를 발급해주고 콜백 (redirect) 주소로 이동합니다.
콜백 주소에서 이렇게 처리했습니다.
function kakaologinprocess(){
$state = $this->input->get_post("state");
$code = $this->input->get_post("code"); //카카오 서버가 발급해준 코드
//엑세스토큰발급
$this->load->library('curl');
$this->curl->create("https://kauth.kakao.com/oauth/token?client_id=".CACAO_APPID."&grant_type=authorization_code&code=".$code."");
$this->curl->execute();
$arrResult = $this->curl->lastResponseRaw();
$arrResult = json_decode($arrResult, true);
if(isset($arrResult["error"])){
echo "로그인에러";
}else{
$access_token = $arrResult["access_token"];
$refresh_token = $arrResult["refresh_token"];
//카카오사용자정보조회
$this->curl->create("https://kapi.kakao.com/v1/user/me");
$this->curl->http_header("Authorization", "Bearer ".$access_token);
$this->curl->execute();
$arrResult = $this->curl->lastResponseRaw();
$profile_json = json_decode($arrResult, true);
if($profile_json["id"]){
$id = $profile_json["id"];
$email = "";
$name = $profile_json["properties"]["nickname"];
$picture = $profile_json["properties"]["profile_image"];
//사용자 프로필정보에 따라 회원가입혹은 업데이트
}
}
}
정상적으로 사용자 인증을 받고 나면 이제 카카오 스토리에 포스팅을 할 수 있습니다.
카카오 스토리에 포스팅해보겠습니다.
제경우에는 멀티이미지와 텍스트를 업로드하는 방식으로 포스팅했습니다.
작업 순서는 이렇습니다.
1. 카카오스토리 사용자인지 확인
2. 포스팅한 이미지를 카카오서버로 업로드
3. 업로드된 이미지와 내용을 포함해서 글쓰기
function kakaopostwrite(){
//1. 카카오스토리 사용자인지 확인
$this->load->library('curl');
$this->curl->create("https://kapi.kakao.com/v1/api/story/isstoryuser");
$this->curl->http_header("Authorization", "Bearer ".사용자인증받고 저장한 엑세스토큰);
$this->curl->execute();
$arrResult = $this->curl->lastResponseRaw();
$arrJson = json_decode($arrResult, true);
//카카오 사용자인경우
if($arrJson["isStoryUser"]){
//2. 포스팅한 이미지를 카카오서버로 업로드
$uploadfile1= "./img/10341483_295646110604581_7030791149797680931_n.png";
$uploadfile2= "./img/KakaoTalk_20141001_134441888.jpg";
$ch = curl_init("https://kapi.kakao.com/v1/api/story/upload/multi");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$dicData["access_token"].""));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file[0]'=>"@$uploadfile1", 'file[1]'=>"@$uploadfile2"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
curl_close($ch);
//3. 업로드된 이미지와 내용을 포함해서 글쓰기
$this->curl->create("https://kapi.kakao.com/v1/api/story/post/photo");
$this->curl->http_header("Authorization", "Bearer ".$dicData["access_token"]);
$post = array("image_url_list"=>$postResult, "content"=>"kakao api로 등록 테스트");
$this->curl->post($post);
$this->curl->execute();
$arrResult = $this->curl->lastResponseRaw();
printr($arrResult);
}
}
마직막에 출력한 값은 포스트의 일련번호 입니다.
이 값은 스토리를 수정하거나 삭제할때 필요합니다.
웹상에서 등록을 테스트 해보고 잘 등록되었나 확인해봤습니다.
[출처] 카카오 스토리 글쓰기 api|작성자 얍소프트
댓글목록
등록된 댓글이 없습니다.