在这篇文章中。我们将使用Ubuntu SDK提供来訪问微博的API并显示所须要的内容。这篇文章的重点是展示怎样在HTML 5中使用online account API。我们将创建一个简单的HTML 5应用。我们终于显示的画面例如以下:
很多其它关于HTML5应用开发的资料能够在地址:
1)创建一个最主要的应用
你能够使用热键Ctrl + R来执行它尽管它并不能做什么事。
2)增加online account所须要的文件
2) weibo.serviceWeibo scope weibo.ubuntu_weibo.desktop Watch your favorite Weibo messages
> <service> <type>sharing</type> <name>Weibo scope</name> <icon>html5-weibo.png</icon> <provider>html5-weibo.ubuntu_account-plugin</provider> <translations>unity-scope-weibo</translations> </service><strong> </strong>
import Ubuntu.OnlineAccounts.Plugin 1.0OAuthMain {}b) qml-weibo.ubuntu_plugin.provider,它的内容为:
> <provider> <name>Weibo</name> <icon>weibo.png</icon> <translations>unity-scope-weibo</translations> <plugin>generic-oauth</plugin> <domains>.*weibo\.com</domains> <single-account>true</single-account> <template> <group name="auth"> <setting name="method">oauth2</setting> <setting name="mechanism">web_server</setting> <group name="oauth2"> <group name="web_server"> <setting name="Host">api.weibo.com</setting> <setting name="AuthPath">oauth2/authorize</setting> <setting name="TokenPath">oauth2/access_token</setting> <setting name="RedirectUri">https://api.weibo.com/oauth2/default.html</setting> <setting name="ResponseType">code</setting> <setting name="ClientId">Your developer key</setting> <setting type="as" name="AllowedSchemes">['https','http']</setting> <setting name="ClientSecret">Your developer secret</setting> <setting name="ForceClientAuthViaRequestBody" type="b">true</setting> </group> </group> </group> </template> </provider>
{ "name": "html5-weibo.ubuntu", "description": "description of html5-weibo", "architecture": "all", "title": "html5-weibo", "hooks": { "html5weibo": { "apparmor": "html5weibo.apparmor", "desktop": "html5weibo.desktop", "account-application": "weibo.application", "account-service": "weibo.service" }, "account-plugin": { "account-provider": "plugin/html5-weibo.ubuntu_account-plugin.provider", "account-qml-plugin": "plugin/qml" } }, "version": "0.1", "maintainer": "XiaoGuo, Liu这里加入了account及plugin的一些设置。", "framework": "ubuntu-sdk-14.10"}
Files { filter: "*.service" } Files { filter: "*.application" } Files { filter: "*.provider" }
3)设计应用的UI
这里的UI很easy,我仅仅使用了一个list控件。我们大部分的代码将在“app.js”中实现:An Ubuntu HTML5 application
var token = '';window.onload = function () { console.log("this is so cool....!") var UI = new UbuntuUI(); UI.init(); UI.pagestack.push('main'); var api = external.getUnityObject('1.0'); var oa = api.OnlineAccounts; // UI.button('getstatus').click(auth); auth(); function auth() { console.log("getstatus button is clicked!"); var filters = {'provider': 'html5-weibo.ubuntu_account-plugin', 'service': ''}; oa.api.getAccounts(filters, function(accounts) { console.log("total length: " + accounts.length); if (accounts.length < 1) { // setResults('No accounts available'); oa.api.requestAccount( "html5-weibo.ubuntu_html5weibo", unescape("html5-weibo.ubuntu_account-plugin"), function() { console.log("requestAccount callback..."); auth(); }); return; } else { // setResults("Available accounts: " + accounts.length); } function authcallback(res) { token = res['data']['AccessToken']; console.log("AccessToken: " + token);// setResults("AccessToken: " + token); getWeiboStatus(token, function(statuses) { console.log("the length: " + statuses.length); if (statuses) { // create the 'ul' element var ul = document.createElement('ul');// var results = document.getElementById('results'); var results = document.querySelector('#results'); var statuslist = document.querySelector('#statuslist'); var ul = document.createElement('ul'); for (var i = 0; i < statuses.length; i ++) { var li = document.createElement('li'); ul.appendChild(li); var aside = document.createElement('aside'); li.appendChild(aside); var img = document.createElement('img'); img.setAttribute('src', statuses[i]['profile_image_url']); img.setAttribute('width', "50"); img.setAttribute('height', "50"); aside.appendChild(img); var a = document.createElement('a'); a.setAttribute('href', '#'); a.innerHTML = statuses[i]['text']; li.appendChild(a); var right = document.createElement('label'); right.innerHTML = "" li.appendChild(right); } console.log("it is done6"); statuslist.appendChild(ul); } else { setResults('ERROR'); } }); } accounts[0].authenticate(authcallback); }); //getAccounts } //auth function getWeiboStatus(accessToken, callback) { var http = new XMLHttpRequest() var url = "https://api.weibo.com/2/statuses/home_timeline.json?access_token=" + accessToken + "&page=" + 1 console.log('url: \n' + url) http.open("GET", url, true); var statuses = []; http.onreadystatechange = function() { if (http.readyState === 4){ if (http.status == 200) { var response = JSON.parse(http.responseText); console.log("it succeeds! lenght: " ); for (i = 0; i < response['statuses'].length; i++ ) { var time = JSON.stringify(response['statuses'][i]['created_at']);// console.log("time: " + time ); var text = JSON.stringify(response['statuses'][i]['text']);// console.log("text: " + text); var name = JSON.stringify(response['statuses'][i]['user']['name']);// console.log("name: " + name); var profile_image_url = JSON.stringify(response['statuses'][i]['user']['profile_image_url']); profile_image_url = profile_image_url.slice(1, profile_image_url.length-1);// console.log("profile_image_url: " + profile_image_url); var id = JSON.stringify(response['statuses'][i]['idstr']); statuses.push({'time': time, 'text': text, 'name': name, 'id': id, 'profile_image_url': profile_image_url}); } callback(statuses); } else { console.log("error: " + http.status) callback(null); } } }; http.send(null); } function setResults(data) { var results = document.getElementById('results'); results.innerHTML = data; };}; //onload我们能够使用例如以下的句子来得到online account API的接口:
var api = external.getUnityObject('1.0'); var oa = api.OnlineAccounts;
var filters = {'provider': 'html5-weibo.ubuntu_account-plugin', 'service': ''};假设没有。我们使用例如以下的代码来创建weibo账号:
if (accounts.length < 1) { // setResults('No accounts available'); oa.api.requestAccount( "html5-weibo.ubuntu_html5weibo", unescape("html5-weibo.ubuntu_account-plugin"), function() { console.log("requestAccount callback..."); auth(); }); return; }等创建成功了,我们使用“auth()”来得到訪问weibo的access token。进而我们能够得到我们所须要的不论什么的信息。
版权声明:本文博主原创文章,博客,未经同意不得转载。