Skip to main content
deleted 54 characters in body; edited tags; edited title
Source Link
Peter O.
  • 33.1k
  • 14
  • 86
  • 97

post Post a tweet with android apiAndroid API

I'm french, so sorry for my bad english. I'mI've been searching since few day howfor a way to post a tweet with my androidAndroid application, but all the things I've found don't work. I

I have to admit that api's twitterTwitter's API isn't so simple to understand, but my code isn't really long, and I don't see where is my mistake.

That'sThis is my code  :

Of course, my keys aren't XXX. And, an other another question, is it possible that, after login, twitter'sto close the Twitter window closed, and bring my activity cometo the foreground.

Best regards?

post a tweet with android api

I'm french, so sorry for my bad english. I'm searching since few day how to post a tweet with my android application, but all the things I've found don't work. I have to admit that api's twitter isn't so simple to understand, but my code isn't really long, and I don't see where is my mistake.

That's my code  :

Of course, my keys aren't XXX. And, an other question, is it possible that, after login, twitter's window closed, and my activity come foreground.

Best regards

Post a tweet with Android API

I've been searching for a way to post a tweet with my Android application, but all the things I've found don't work.

I have to admit that Twitter's API isn't so simple to understand, but my code isn't really long, and I don't see where is my mistake.

This is my code:

Of course, my keys aren't XXX. And another question, is it possible, after login, to close the Twitter window and bring my activity to the foreground?

edited tags
Link
deleted 4 characters in body
Source Link
 public class PartagerTwitter extends Activity { private CommonsHttpOAuthConsumer httpOauthConsumer; private OAuthProvider httpOauthprovider; public final static String consumerKey = XXX; public final static String consumerSecret = XXX; private final String CALLBACKURL = "myapp://twitactivity"; private Twitter twitter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); httpOauthprovider = new DefaultOAuthProvider("https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token", "https://twitter.com/oauth/authorize"); String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL); this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); } catch (Exception e) { e.printStackTrace(); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Uri uri = intent.getData(); Log.i("test", uri.toString()); if (uri != null && uri.toString().startsWith(CALLBACKURL)) { String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); try { httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier); twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(consumerKey, consumerSecret); RequestToken requestToken = twitter.getOAuthRequestToken(CALLBACKURL); AccessToken accessToken = twitter.getOAuthAccessToken(requestToken); String token = accessToken.getToken(), secret = accessToken.getTokenSecret(); accessToken = new AccessToken(token,secret); Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey,consumerSecret,accessToken); twitter.updateStatus("My First Status Update"); } catch (Exception e) { e.printStackTrace(); } } } } 
 public class PartagerTwitter extends Activity { private CommonsHttpOAuthConsumer httpOauthConsumer; private OAuthProvider httpOauthprovider; public final static String consumerKey = XXX; public final static String consumerSecret = XXX; private final String CALLBACKURL = "myapp://twitactivity"; private Twitter twitter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); httpOauthprovider = new DefaultOAuthProvider("https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token", "https://twitter.com/oauth/authorize"); String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL); this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); } catch (Exception e) { e.printStackTrace(); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Uri uri = intent.getData(); Log.i("test", uri.toString()); if (uri != null && uri.toString().startsWith(CALLBACKURL)) { String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); try { httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier); twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(consumerKey, consumerSecret); RequestToken requestToken = twitter.getOAuthRequestToken(CALLBACKURL); AccessToken accessToken = twitter.getOAuthAccessToken(requestToken); String token = accessToken.getToken(), secret = accessToken.getTokenSecret(); accessToken = new AccessToken(token,secret); Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey,consumerSecret,accessToken); twitter.updateStatus("My First Status Update"); } catch (Exception e) { e.printStackTrace(); } } } } 
public class PartagerTwitter extends Activity { private CommonsHttpOAuthConsumer httpOauthConsumer; private OAuthProvider httpOauthprovider; public final static String consumerKey = XXX; public final static String consumerSecret = XXX; private final String CALLBACKURL = "myapp://twitactivity"; private Twitter twitter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); httpOauthprovider = new DefaultOAuthProvider("https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token", "https://twitter.com/oauth/authorize"); String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL); this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); } catch (Exception e) { e.printStackTrace(); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Uri uri = intent.getData(); Log.i("test", uri.toString()); if (uri != null && uri.toString().startsWith(CALLBACKURL)) { String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); try { httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier); twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(consumerKey, consumerSecret); RequestToken requestToken = twitter.getOAuthRequestToken(CALLBACKURL); AccessToken accessToken = twitter.getOAuthAccessToken(requestToken); String token = accessToken.getToken(), secret = accessToken.getTokenSecret(); accessToken = new AccessToken(token,secret); Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey,consumerSecret,accessToken); twitter.updateStatus("My First Status Update"); } catch (Exception e) { e.printStackTrace(); } } } } 
fixed code
Source Link
Will Tate
  • 33.5k
  • 9
  • 80
  • 71
Loading
Source Link
Loading