Skip to content

Instantly share code, notes, and snippets.

@takimo
Created December 22, 2010 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takimo/750866 to your computer and use it in GitHub Desktop.
Save takimo/750866 to your computer and use it in GitHub Desktop.
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
/* 設定してください */
// https://sap.mixi.jp/connect_consumer.pl
var CONSUMER_KEY = "";
var CONSUMER_SECRET = "";
var REDIRECT_URL = "";
// http://developer.mixi.co.jp/connect/mixi_graph_api/api_auth
var SCOPE = "r_voice";
/* 設定してください */
var _access_token = "";
var _refresh_token = "";
var _scope = "";
var tableView = Ti.UI.createTableView({
data:[],
minRowHeight:70
});
//
// create controls tab and root window
//
var win2 = Titanium.UI.createWindow({
title:'Timeline',
backgroundColor:'#fff'
});
var tab2 = Titanium.UI.createTab({
icon:'KS_nav_ui.png',
title:'Timeline',
window:win2
});
var gup = function(url, name){
var results = (new RegExp("[\\?&]"+name+"=([^&#]*)")).exec(url);
if(results == null){
return "";
}else{
return results[1];
}
};
var postXHR = function(url, options, callback){
var xhr = Ti.Network.createHTTPClient();
xhr.open(options.method, url, true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", options.param.length);
xhr.setRequestHeader("Connection", "close");
xhr.onload = function() {
var responseText = this.responseText;
callback(responseText);
};
xhr.send(options.param);
};
var getRecentVoice = function(){
var url = "http://api.mixi-platform.com/2/voice/statuses/friends_timeline/";
var xhr = Ti.Network.createHTTPClient();
xhr.open("GET", url, true);
xhr.setRequestHeader("Authorization", "OAuth " + _access_token);
xhr.onload = function(){
var timeline = JSON.parse(this.responseText);
updateTimeline(timeline);
tab2.show();
alert("認証に成功しました。Timelineタブをひらいてください");
};
xhr.send();
}
var updateTimeline = function(timeline){
var currentData = [];
for (var i=0;i<timeline.length;i++){
var entry = timeline[i];
var row = Ti.UI.createTableViewRow({hasChild:true,height:'auto'});
var ProfileImage = Ti.UI.createImageView({
image:entry.user.profile_image_url,
left:0,
top:0,
height:48,
width:48
});
row.add(ProfileImage);
var commentLabel = Ti.UI.createLabel({
text: entry.text,
left:54,
top:0,
bottom:2,
height:'auto',
width:236,
textAlign:'left',
font:{fontSize:14}
});
row.add(commentLabel);
row.className = 'entry_row';
currentData.push(row);
}
tableView.setData(currentData);
}
var checkAuthorization = function(url, param, callback){
postXHR(url, {method: "POST", param: param}, function(responseText){
var results = JSON.parse(responseText);
_access_token = results.access_token;
_refresh_token = results.refresh_token;
_scope = results.scope;
getRecentVoice();
});
};
var AUTHORIZATION_URL = "https://mixi.jp/connect_authorize.pl";
var AUTHORIZATION_PARAM = "?client_id=" + CONSUMER_KEY + "&response_type=code&scope=" + SCOPE;
var window = Titanium.UI.createWindow();
var webview = Titanium.UI.createWebView({
url:AUTHORIZATION_URL + AUTHORIZATION_PARAM}
);
webview.addEventListener('load', function(e){
if(!e.url.match(REDIRECT_URL)) return;
var url = "https://secure.mixi-platform.com/2/token";
var param = "grant_type=authorization_code&client_id=" + CONSUMER_KEY + "&client_secret=" + CONSUMER_SECRET + "&code=" + gup(e.url, "code") + "&redirect_uri=" + REDIRECT_URL;
checkAuthorization(url, param, getRecentVoice)
});
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({
title:'Auth',
backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({
icon:'KS_nav_views.png',
title:'Auth',
window:win1
});
var label1 = Titanium.UI.createLabel({
color:'#999',
text:'I am Window 1',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto'
});
win1.add(label1);
win1.add(webview);
win2.add(tableView);
//
// add tabs
//
tabGroup.addTab(tab1);
tabGroup.addTab(tab2);
// open tab group
tabGroup.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment