I try to write an application where an user can log in on twitter. I use twitter4j like library.My problem is that when I go in the page where I must put username and password, the program block because i don't know use callback to came in my application. Someone can me help?
public class MainActivity extends Activity {
private Twitter twitter;
RequestToken requestToken;
final public static String CALLBACK_SCHEME = "x-latify-oauth-twitter";
final public static String CALLBACK_URL = CALLBACK_SCHEME + "://callback";
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new updateTwitterStatus().execute();
}
});
}
@Override
protected void onDestroy() {
twitter.shutdown();
}
class updateTwitterStatus extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String testStatus = "prova tweet ";
ConfigurationBuilder cb = new ConfigurationBuilder();
// the following is set without accesstoken- desktop client
cb.setDebugEnabled(true)
.setOAuthConsumerKey("******")
.setOAuthConsumerSecret(
"*****");
try {
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
Log.i("bauu", "miao");
requestToken = twitter.getOAuthRequestToken();
String authUrl = requestToken.getAuthenticationURL();
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(requestToken.getAuthenticationURL())));
uri = Uri.parse(requestToken.getAuthenticationURL());
return authUrl;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(s)));
}
}
Make sure your callback URL in twitter dev app options are as follows,
and within your android manifest file, in between the of the actvitiy that takes you to twitter, make sure you define:
lastly, make sure in your program,
Twitter login in 4 easy steps:
1- Add intent-filter for your activity (Based on @rennoDeniro response)
AndroidManifest.xml
2- Define twitter key and secret in
strings.xml
3- Request twitter for signup page in
MainActivity.java
4- Handle Callback in
MainActivity.java