1 미투데이 JS 라이브러리
사용법에 앞서서 기본적으로 me2day APIKEY 발급 받는다.
2 me2day APIKey 발급
- me2day APIKey 발급
- 아래의 양식에 따라 어플리케이션을 생성합니다. (한번 입력한 내용은 수정이 불가합니다.)
- http://me2day.net/me2/app/key/list 로 이동 후 새로 발급받은 어플리케이션 설정을 클릭 후 아래와 같이 설정합니다.
쉬운 인증 설정 -> 웹 기반 인증
콜백주소 (URL) : domain주소/me2Callback.html
- 발급받은 API KEY를 기억 후에 실제 구동할 사이트에서 아래의 JS를 추가합니다.
3 JS 사용
- 아래와 같은 코드를 삽입하여 me2데이 객체를 생성합니다.
var oMe2 = new me2day(‘발급받은 API KEY’);
- 위의 객체를 생성 후 아래와 같이 Call한 후 실제로 로그인이 동작되는지 확인 해봅니다.
oMe2.getLogin();
- 성공적으로 로그인이 되면 아래와 같은 방식으로 API를 Call 합니다.
미투데이 경우는 인증과 비인증시 호출할 수 있는 API가 따로 정의 되어있습니다. 자세한건 Naver me2day API 페이지에서 확인하시면 됩니다.
- 인증시 호출하는 방법
oMe2.API명( JSON , callbackFunc);
예)
oMe2.create_post( { 'post[body]' : '하이!!' }, function(res){ console.log(res) } );
- 비 인증시 호출하는 방법
oMe2.API명 (JSON , callbackFunc);
API명 : 인증시와 동일
JSON : 해당 API 에 맞는 변수를 선언해주면 되며, get_person과 같은 경우는 {user_id : 아이디}를 추가하시면 됩니다.
callbackFunc: 리턴받을 callback 함수를 정의합니다.
성공 : 미투데이 API에 정의된 리턴 값
실패 : false
예)
oMe2.get_person( {user_id : 'dongz1'}, function(res){ console.log(res) } );
- 참고
쿠키가 사용되지 않은 버전이라 새로고침시 새로 로그인하셔야 합니다!
window.__me2day_callback = {};
window.__me2day_returnUrl = '';
function me2day (key) {
return {
API_URL : "http://me2day.net/api/",
me2ApiKey : key,
oUser : {
uid : null,
ukey : null
},
ajax : function(sUrl, oOption) {
var id = "_" + Math.floor(Math.random() * 10000);
var _script ;
var timeout = true;
window.__me2day_callback[id] = function(oCallBackData){
try {
timeout = false;
oOption.callback(oCallBackData);
_script.parentNode.removeChild(_script);
} finally {
delete window.__me2day_callback[id];
}
}
var head = document.getElementsByTagName("head")[0];
_script = document.createElement("script");
_script.type = "text/javascript";
_script.charset = "utf-8";
if (head) {
head.appendChild(_script);
} else if (document.body) {
document.body.appendChild(_script);
}
var data = [];
for(var i in oOption){
if(oOption.hasOwnProperty(i) && i != "callback" && i != "failback"){
data.push(i+"="+encodeURIComponent(oOption[i]));
}
}
if(data.length == 0){
_script.src = sUrl+"?callback=window.__me2day_callback[\""+id+"\"]";
}else{
_script.src = sUrl+"?"+data.join("&")+"&callback=window.__me2day_callback[\""+id+"\"]";
}
window.setTimeout(function(){
if(timeout){
oOption.callback(false);
}
}, 3000);
},
getLogin : function(){
var _this = this;
var sUrl = this.API_URL + 'get_auth_url.json';//?akey='+me2ApiKey;
var Z = 1040, h = 720;
var c = screen.height;
var b = screen.width;
var a = Math.round((b / 2) - (Z / 2));
var g = 0;
if (c > h) {
g = Math.round((c / 2) - (h / 2));
}
var Y = '';
var oOpen = window.open('','me2_login', "left=" + a + ",top=" + g + ",width=" + Z + ",height=" + h + ",personalbar=0,toolbar=0,scrollbars=0,resizable=1");
var oOption = {
akey : this.me2ApiKey,
callback : function(res){
oOpen.location.href = res.url;
var checkWindowArray = new Array();
checkWindow = setInterval(function(){
if(oOpen.closed) {
oOpen = undefined;
for(var i in checkWindowArray){
clearInterval(checkWindowArray.pop());
}
try{
var aMe2data = new Array();
aMe2data = __me2day_returnUrl.split('&');
if(aMe2data[1].split('=')[1] == 'true') {
_this.oUser.uid = aMe2data[2].split('=')[1];
_this.oUser.ukey = aMe2data[3].split('=')[1];
}else{
alert('error');
}
}catch(e){
}
}
},100);
checkWindowArray.push(checkWindow);
}
};
_this.ajax(sUrl,oOption);
},
getApiNone : function(sType, oParam, cb){
var sUrl = this.API_URL + sType;
if (oParam.user_id) {
sUrl += "/"+oParam.user_id+".json";
}else{
sUrl += "/"+this.oUser.uid+".json";
}
oParam.callback = cb;
this.ajax(sUrl,oParam);
},
getApiAuth : function(sType, oParam, cb){
if(this.oUser.uid == null){
alert('사용자 인증후 사용하세요.');
return false;
}
var sUrl = this.API_URL + sType;
sUrl += "/"+this.oUser.uid+".json";
oParam.callback = function(res){
if(res.code){
cb(res);
}else{
cb(res);
}
};
oParam.uid = this.oUser.uid;
var nonce = '12345678';
oParam.ukey = nonce+hex_md5(nonce + this.oUser.ukey);
oParam.akey = this.me2ApiKey;
this.ajax(sUrl,oParam);
},
get_posts : function(oParam, cb){
this.getApiNone("get_posts",oParam, cb);
},
create_post : function(oParam, cb){
this.getApiAuth("create_post",oParam, cb)
},
delete_post : function(oParam, cb){
this.getApiAuth("delete_post",oParam, cb)
},
create_comment: function(oParam, cb){
this.getApiAuth("create_comment",oParam, cb)
},
get_comments : function(oParam, cb){
this.getApiAuth("get_comments",oParam, cb)
},
delete_comment : function(oParam, cb){
this.getApiAuth("delete_comment",oParam, cb)
},
track_comments : function(oParam, cb){
this.getApiAuth("track_comments",oParam, cb)
},
metoo : function(oParam, cb){
this.getApiAuth("metoo",oParam, cb)
},
get_metoos: function(oParam, cb){
this.getApiNone("get_metoos",oParam, cb)
},
get_friends : function(oParam, cb){
this.getApiNone("get_friends",oParam, cb)
},
get_friendship_requests : function(oParam, cb){
this.getApiAuth("get_friendship_requests",oParam, cb)
},
accept_friendship_request : function(oParam, cb){
this.getApiAuth("accept_friendship_request",oParam, cb)
},
friendship : function(oParam, cb){
this.getApiAuth("friendship",oParam, cb)
},
get_person : function(oParam, cb){
this.getApiNone("get_person",oParam, cb)
},
get_setting : function(oParam, cb){
this.getApiAuth("get_setting",oParam, cb)
},
get_bands : function(oParam, cb){
this.getApiAuth("get_bands",oParam, cb)
},
noop : function(oParam, cb){
this.getApiAuth("noop",oParam, cb)
}
};
}