我有一个Phonegap应用程序,它使用ChildBrowser来授权我的应用程序使用Oauth与用户Twitter帐户建立连接。一旦用户登录他们的Twitter帐户授权应用程序连接,Twitter就会将ChildBrowser发送到回调网址,在那里我从网址检索request_token。但后来我尝试调用API来为access_token交换request_token,并且应用程序不会继续超过该点。看起来应用程序在API调用期间断开以获取access_token但我无法看到如何。
任何帮助都将非常感谢!!!
非常感谢你们!
这是我的javascript:
/* -- Twitter START -- */
var Twitter = {
init:function() {
var oauth;
var requestParams;
var options = {
consumerKey: 'blahblah',
consumerSecret: 'blahblah',
callbackUrl: "http://beaconize.com/"
};
alert(localStorage.twitterKey);
var cb = ChildBrowser.install(); // install our ChildBrowser ( cb )
var twitterKey = "twttrKey"; // what we will store our twitter user information in
// our storedAccessData and Raw Data
var storedAccessData, rawData = localStorage.getItem(twitterKey);
// First thing we need to do is check to see if we already have the user saved!
if(localStorage.getItem(twitterKey) !== null){
// If we already have them
storedAccessData = JSON.parse(rawData); // Parse our JSON object
options.accessTokenKey = storedAccessData.accessTokenKey; // This is saved when they first sign in
options.accessTokenSecret = storedAccessData.accessTokenSecret; // this is saved when they first sign in
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);
alert("USERNAME: " + entry.screen_name);
}
);
} else {
// We don't have a user saved yet
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); }; // When the ChildBrowser URL changes we need to track that
},
function(data) {
alert("ERROR: "+data);
}
);
}
},
/*
When The ChildBrowser URL changes we will track it here.
We will also determine if the request was a success or not here
*/
success:function(loc) {
// The supplied oauth_callback_url for this session is being loaded
/*
We will check to see if the childBrowser's new URL matches our callBackURL
*/
if (loc.indexOf("http://beaconize.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];
}
}
// After the next line, nothing executes. It stops in ChildBrowser on my callback_url.
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]);
}
oauth.setAccessToken([accessParams.oauth_token, accessParams.oauth_token_secret]);
// Save access token/key in localStorage
var accessData = {};
accessData.accessTokenKey = accessParams.oauth_token;
accessData.accessTokenSecret = accessParams.oauth_token_secret;
// SETTING OUR LOCAL STORAGE
alert("TWITTER: Storing token key/secret in localStorage3");
localStorage.setItem(twitterKey, JSON.stringify(accessData));
},
function(data) {
console.log(data);
}
);
oauth.get('https://api.twitter.com/1/account/verify_credentials.json?skip_status=true',
function(data) {
var entry = JSON.parse(data.text);
alert("TWITTER USER: "+entry.screen_name);
// FOR EXAMPLE ONLY
//app.init();
},
function(data) {
alert("ERROR: " + data);
}
);
// Since everything went well we can close our childBrowser!
window.plugins.childBrowser.close();
} else {
// do nothing
}
},
tweet:function() {
var storedAccessData, rawData = localStorage.getItem(twitterKey);
storedAccessData = JSON.parse(rawData); // Parse our JSON object
options.accessTokenKey = storedAccessData.accessTokenKey; // This is saved when they first sign in
options.accessTokenSecret = storedAccessData.accessTokenSecret; // this is saved when they first sign in
// jsOAuth takes care of everything for us we just need to provide 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();
}
);
},
/*
Now that we have the information we can Tweet!
*/
post:function() {
var theTweet = $("#tweet").val(); // Change this out for what ever you want!
oauth.post('https://api.twitter.com/1/statuses/update.json',
{ 'status' : theTweet, // jsOAuth encodes for us
'trim_user' : 'true' },
function(data) {
var entry = JSON.parse(data.text);
alert(entry);
// FOR THE EXAMPLE
app.done();
},
function(data) {
alert(data);
}
);
}
};
/* -- Twitter END -- */
答案 0 :(得分:1)
好的,所以我将您的代码与您引用的原始项目进行了比较。这就是我网络化的地方。
看起来你错过了一些导致这种情况发生的事情。
这是原始代码 - 这应该可行
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]);
}
oauth.setAccessToken([accessParams.oauth_token, accessParams.oauth_token_secret]);
// Save access token/key in localStorage
var accessData = {};
accessData.accessTokenKey = accessParams.oauth_token;
accessData.accessTokenSecret = accessParams.oauth_token_secret;
// SETTING OUR LOCAL STORAGE
alert("TWITTER: Storing token key/secret in localStorage3");
localStorage.setItem(twitterKey, JSON.stringify(accessData));
},
function(data) {
console.log(data);
}
);
oauth.get('https://api.twitter.com/1/account/verify_credentials.json?skip_status=true',
function(data) {
var entry = JSON.parse(data.text);
alert("TWITTER USER: "+entry.screen_name);
// FOR EXAMPLE ONLY
//app.init();
},
function(data) {
alert("ERROR: " + data);
}
);
// Since everything went well we can close our childBrowser!
window.plugins.childBrowser.close();
} else {
// do nothing
}
},
有一点需要注意,这就是你的代码不同的地方是第一个oauth请求的错误函数 - 你的代码似乎缺少这个,这不是什么大不了的事 - 但是你已经取出格式化了所以那两个功能现在是导致问题的功能。
如果您复制此代码并选择并粘贴代码并切换它们,您将看到导致问题的细微差别!
答案 1 :(得分:0)
到目前为止,查看您的代码,一切看起来都很合理。
我注意到你正在手动做一些auth-dance工作。您是否尝试过使用fetchRequestToken()&amp; fetchAccessToken()方法?我写了boiler plate code to demonstrate this。
你能给出错误信息的输出吗?通常它给出了Twitter没有授权你的原因。
您可能会发现这个有用的API reference还有一个未记录的方法来解析url字符串parseTokenRequest()