我正在开发Twitter -Client的应用程序。我从这个网站得到了很多提示。我写了一些来吧
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
public class TwitterConnetcion extends Activity {
private static final String APP ="OAUTH";
private Twitter twitter;
private OAuthProvider provider;
private CommonsHttpOAuthConsumer consumer;
private String CONSUMER_KEY ="MyConsumerKey";
private String CONSUMER_SECRET ="MyConsumersecrete";
private String CALLBACK_URL ="SoftDroidbyDhrumil://twitterconnetcion";///SoftDroid is my twitter //Apllication that i registered in Twitter site
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider("https://api.twitter.com/oauth/request_token","https://api.twitter.com/oauth/access_token","https://api.twitter.com/oauth/authorize");
String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Log.i("Dhrumil",authUrl);
Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
Log.i("Dhrumil","Browser Start"); } catch (Exception e) {
Log.i("Dhrumil"+APP, e.toString());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
Log.i("Dhrumil verifier",verifier);
try {
// this will populate token and token_secret in consumer
provider.retrieveAccessToken(consumer, verifier);
// TODO: you might want to store token and token_secret in you app settings!!!!!!!!
AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());
Log.i("Dhrumil AccessToken",a.toString());
// initialize Twitter4J
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
// create a tweet
Date d = new Date(System.currentTimeMillis());
String tweet = "#OAuth working! " + d.toLocaleString();
// send the tweet
twitter.updateStatus(tweet);
// feedback for the user...
Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e(APP, e.getMessage());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}}
我编译好并运行此应用程序,它将在Twitter网站上重定向我以获得“允许”权限。然后我想回到我的Android应用程序,但我得到错误“找不到”SoftDroidbyDhrumil:// twitterconnetcion?OuathToken = somoething“
我在CallBack网址上写了什么,以便我可以从Android浏览器回到我的应用程序?
答案 0 :(得分:4)
您需要对应用在访问时处理的特定网址进行意图过滤。
因此,您将重定向到以下内容:
yourcustomscheme:// witterconnetcion OuathToken = somoething
在下一页的中间,描述了它 http://developer.android.com/guide/topics/intents/intents-filters.html