Javascript 如何将twitter与phonegap ios集成
我正在使用phonegap 2.9.0,并试图为我正在为ios构建的应用程序实现twitter 我正在关注这一点,但运气不太好。使用实现的子浏览器,并且由于其cordovo.plst已被弃用,因此在config.xml中添加了以下行Javascript 如何将twitter与phonegap ios集成,javascript,ios,twitter,cordova,phonegap-plugins,Javascript,Ios,Twitter,Cordova,Phonegap Plugins,我正在使用phonegap 2.9.0,并试图为我正在为ios构建的应用程序实现twitter 我正在关注这一点,但运气不太好。使用实现的子浏览器,并且由于其cordovo.plst已被弃用,因此在config.xml中添加了以下行 <feature name="ChildBrowserCommand"> <param name="ios-package" value="ChildBrowserCommand" /> </feature> 在我的
<feature name="ChildBrowserCommand">
<param name="ios-package" value="ChildBrowserCommand" />
</feature>
在我的index.html文件中
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta charset="utf-8">
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jsOAuth-1.3.6.min.js"></script>
<script type="text/javascript" charset="utf-8" src="ChildBrowser.js"></script>
<script type="text/javascript">
function onBodyLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
var root = this;
cb = window.plugins.childBrowser;
if (!localStorage.getItem(twitterKey)) {
$("#loginBtn").show();
$("#logoutBtn").hide();
}
else {
$("#loginBtn").hide();
$("#logoutBtn").show();
}
if (cb != null) {
cb.onLocationChange = function(loc) {
root.locChanged(loc);
};
cb.onClose = function() {
root.onCloseBrowser()
};
cb.onOpenExternal = function() {
root.onOpenExternal();
};
}
}
function onCloseBrowser() {
console.log("onCloseBrowser!");
}
function locChanged(loc) {
console.log("locChanged!");
}
function onOpenExternal() {
console.log("onOpenExternal!");
}
</script>
<!--Below is the code for twitter-->
<script>
// GLOBAL VARS
var oauth; // It Holds the oAuth data request
var requestParams; // Specific param related to request
var options = {
consumerKey: 'CONSUMER KEy', // YOUR Twitter CONSUMER_KEY
consumerSecret: 'CONSUMER_SECRET', // YOUR Twitter CONSUMER_SECRET
callbackUrl: "http://www.textalert.com/"}; // YOU have to replace it on one more Place
var twitterKey = "twtrKey"; // This key is used for storing Information related
var Twitter = {
init: function() {
// Apps storedAccessData , Apps Data in Raw format
var storedAccessData, rawData = localStorage.getItem(twitterKey);
// here we are going to check whether the data about user is already with us.
if (localStorage.getItem(twitterKey) !== null) {
// when App already knows data
storedAccessData = JSON.parse(rawData); //JSON parsing
//options.accessTokenKey = storedAccessData.accessTokenKey; // data will be saved when user first time signin
options.accessTokenSecret = storedAccessData.accessTokenSecret; // data will be saved when user first first signin
// javascript OAuth take care of everything for app we need to provide just the options
oauth = OAuth(options);
oauth.get('https://api.twitter.com/1/account/verify_credentials.json?skip_status=true',
function(data) {
var entry = JSON.parse(data.text);
console.log("USERNAME: " + entry.screen_name);
}
);
}
else {
// we have no data for save user
oauth = OAuth(options);
oauth.get('https://api.twitter.com/oauth/request_token',
function(data) {
requestParams = data.text;
cb.showWebPage('https://api.twitter.com/oauth/authorize?' + data.text); // This opens the Twitter authorization / sign in page
cb.onLocationChange = function(loc) {
Twitter.success(loc);
}; // Here will will track the change in URL of ChildBrowser
},
function(data) {
console.log("ERROR: " + data);
}
);
}
},
/*
When ChildBrowser's URL changes we will track it here.
We will also be acknowledged was the request is a successful or unsuccessful
*/
success: function(loc) {
// Here the URL of supplied callback will Load
/*
Here Plugin will check whether the callback Url matches with the given Url
*/
if (loc.indexOf("http://www.textalert.com/?") >= 0) {
// Parse the returned URL
var index, verifier = '';
var params = loc.substr(loc.indexOf('?') + 1);
params = params.split('&');
for (var i = 0; i < params.length; i++) {
var y = params[i].split('=');
if (y[0] === 'oauth_verifier') {
verifier = y[1];
}
}
// Here we are going to change token for request with token for access
/*
Once user has authorised us then we have to change the token for request with token of access
here we will give data to localStorage.
*/
oauth.get('https://api.twitter.com/oauth/access_token?oauth_verifier=' + verifier + '&' + requestParams,
function(data) {
var accessParams = {};
var qvars_tmp = data.text.split('&');
for (var i = 0; i < qvars_tmp.length; i++) {
var y = qvars_tmp[i].split('=');
accessParams[y[0]] = decodeURIComponent(y[1]);
}
$('#oauthStatus').html('<span style="color:green;">Success!</span>');
$('#stage-auth').hide();
$('#stage-data').show();
oauth.setAccessToken([accessParams.oauth_token, accessParams.oauth_token_secret]);
// Saving token of access in Local_Storage
var accessData = {};
accessData.accessTokenKey = accessParams.oauth_token;
accessData.accessTokenSecret = accessParams.oauth_token_secret;
// Configuring Apps LOCAL_STORAGE
console.log("TWITTER: Storing token key/secret in localStorage");
localStorage.setItem(twitterKey, JSON.stringify(accessData));
oauth.get('https://api.twitter.com/1/account/verify_credentials.json?skip_status=true',
function(data) {
var entry = JSON.parse(data.text);
console.log("TWITTER USER: " + entry.screen_name);
$("#welcome").show();
document.getElementById("welcome").innerHTML = "welcome " + entry.screen_name;
successfulLogin();
// Just for eg.
app.init();
},
function(data) {
console.log("ERROR: " + data);
}
);
// Now we have to close the child browser because everthing goes on track.
window.plugins.childBrowser.close();
},
function(data) {
console.log(data);
}
);
}
else {
// Just Empty
}
},
tweet: function() {
var storedAccessData, rawData = localStorage.getItem(twitterKey);
storedAccessData = JSON.parse(rawData); // Paring Json
options.accessTokenKey = storedAccessData.accessTokenKey; // it will be saved on first signin
options.accessTokenSecret = storedAccessData.accessTokenSecret; // it will be save on first login
// javascript OAuth will care of else for app we need to send only the options
oauth = OAuth(options);
oauth.get('https://api.twitter.com/1/account/verify_credentials.json?skip_status=true',
function(data) {
var entry = JSON.parse(data.text);
Twitter.post();
}
);
},
/*
We now have the data to tweet
*/
post: function() {
var theTweet = $("#tweet").val(); // You can change it with what else you likes.
oauth.post('https://api.twitter.com/1/statuses/update.json',
{'status': theTweet, // javascript OAuth encodes this
'trim_user': 'true'},
function(data) {
var entry = JSON.parse(data.text);
console.log(entry);
// just for eg.
done();
},
function(data) {
console.log(data);
}
);
}
}
function done() {
$("#tweet").val('');
}
function successfulLogin() {
$("#loginBtn").hide();
$("#logoutBtn,#tweet,#tweeter,#tweetBtn,#tweetText").show();
}
function logOut() {
//localStorage.clear();
window.localStorage.removeItem(twitterKey);
document.getElementById("welcome").innerHTML = "Please Login to use this app";
$("#loginBtn").show();
$("#logoutBtn,#tweet,#tweeter,#tweetText,#tweetBtn").hide();
}
</script>
<!--Code for Twitter ends here-->
</head>
<body onload="onBodyLoad()">
<h4>Oodles Twitter App</h4>
<table border="1">
<tr>
<th>Login using Twitter</th>
<th>
<button id="loginBtn" onclick="Twitter.init()">Login</button>
<button id="logoutBtn" onclick="logOut();">Logout</button>
</th>
</tr>
<tr id="tweetText" style="display:none;">
<td colspan="2"><textarea id="tweet" style="display:none;"></textarea></td>
</tr>
<tr id="tweetBtn" style="display:none;">
<td colspan="2" align="right">
<button id="tweeter" onclick="Twitter.tweet();" style="display:none">Tweet</button>
</td>
</tr>
<tr><td colspan="2"><div id="welcome">Please Login to use this app</div></td></tr>
</table>
</body>
</html>
函数onBodyLoad(){
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
}
函数ondevicerady(){
var root=这个;
cb=window.plugins.childBrowser;
如果(!localStorage.getItem(twitterKey)){
$(“#loginBtn”).show();
$(“#logoutBtn”).hide();
}
否则{
$(“#loginBtn”).hide();
$(“#logoutBtn”).show();
}
如果(cb!=null){
cb.onLocationChange=功能(loc){
根。locChanged(loc);
};
cb.onClose=函数(){
root.onCloseBrowser()
};
cb.onOpenExternal=函数(){
根,外根();
};
}
}
函数onCloseBrowser(){
log(“onCloseBrowser!”);
}
功能更改(loc){
console.log(“locChanged!”);
}
外周功能(){
log(“onOpenExternal!”);
}
//全局变量
var oauth;//它保存oAuth数据请求
var requestParams;//与请求相关的特定参数
变量选项={
consumerKey:'消费者密钥',//您的Twitter消费者密钥
ConsumerCreet:'消费者秘密',//你的Twitter消费者秘密
回调URL:“http://www.textalert.com/"}; // 你必须再换一个地方
var twitterKey=“twtrKey”;//此键用于存储相关信息
变量Twitter={
init:function(){
//Apps storedAccessData,原始格式的应用程序数据
var storedAccessData,rawData=localStorage.getItem(twitterKey);
//在这里,我们将检查有关用户的数据是否已经存在。
if(localStorage.getItem(twitterKey)!==null){
//当应用程序已经知道数据时
storedAccessData=JSON.parse(rawData);//JSON解析
//options.accessTokenKey=storedAccessData.accessTokenKey;//当用户首次登录时,数据将被保存
options.accessTokenSecret=storedAccessData.accessTokenSecret;//当用户首次登录时,将保存数据
//javascript OAuth负责应用程序的一切,我们只需要提供选项
oauth=oauth(选项);
oauth.get('https://api.twitter.com/1/account/verify_credentials.json?skip_status=true',
功能(数据){
var entry=JSON.parse(data.text);
console.log(“用户名:”+entry.screen\u name);
}
);
}
否则{
//我们没有保存用户的数据
oauth=oauth(选项);
oauth.get('https://api.twitter.com/oauth/request_token',
功能(数据){
requestParams=data.text;
cb.展示网页('https://api.twitter.com/oauth/authorize?“+data.text);//这将打开Twitter授权/登录页面
cb.onLocationChange=功能(loc){
Twitter.success(loc);
};//此处将跟踪ChildBrowser的URL中的更改
},
功能(数据){
console.log(“错误:+数据”);
}
);
}
},
/*
当ChildBrowser的URL更改时,我们将在此处跟踪它。
我们还将确认请求是否成功或不成功
*/
成功:功能(loc){
//这里将加载提供的回调的URL
/*
在此插件将检查回调Url是否与给定Url匹配
*/
如果(位置索引)http://www.textalert.com/?") >= 0) {
//解析返回的URL
var指数,验证人=“”;
变量参数=loc.substr(loc.indexOf(“?”)+1);
参数=参数拆分('&');
对于(变量i=0;i