private static String requestToken;
private static String requestSecret;
public static void loginTwitter(Context mycontext) {
if (savedToken==null) {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token",
"https://a开发者_开发问答pi.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Toast.makeText(mycontext, "login to twitter", Toast.LENGTH_LONG).show();
mycontext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
} catch (Exception e) {
Log.e(APP, e.getMessage());
}
} else {
tweetShareURL();
}
}
private static void tweetShareURL() {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(savedToken);
// create a tweet
String tweet = title+" " + url;
try {
twitter.updateStatus(tweet);
} catch (TwitterException e) {
Log.e(APP, "2 "+e.getMessage());
} finally {
utils.hint("tweet sent!");
}
}
static AccessToken savedToken=null;
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
Log.e("URI", ""+uri.toString());
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
Log.e("verifier", ""+verifier);
try {
requestToken = consumer.getToken();
requestSecret = consumer.getConsumerSecret();
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
consumer.setTokenWithSecret(requestToken, requestSecret);
provider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
provider.setOAuth10a(true);
// TODO: you might want to store token and token_secret in you app settings!!!!!!!!
savedToken = new AccessToken(requestToken, requestSecret);
provider.retrieveAccessToken(consumer, verifier);
Log.e(APP, "2");
tweetShareURL();
} catch (Exception e) {
Log.e(APP, "1 "+e.getMessage());
}
}
}
always get this lines:
11-02 22:36:39.526: ERROR/OAUTH(424): 2 401:Authentication credentials were missing or incorrect. 11-02 22:36:39.526: ERROR/OAUTH(424): {"error":"Invalid \/ expired Token","request":"\/1\/statuses\/update.json"}
why?
"\/1\/statuses\/update.json"
When you have things in quotes it sends everything with in the quotes as string. So the escape character \
perhaps is getting in the way?
精彩评论