-
-
Save vivdub/97ed32efcada70516cc9 to your computer and use it in GitHub Desktop.
This will demonstrate how to use twitter in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TwitterShare extends Activity { | |
/** Called when the activity is first created. */ | |
public static final String CONSUMER_KEY = "your consumer key"; | |
public static final String CONSUMER_SECRET = "your secret key"; | |
public static final String CALLBACK_URL = "http://localhost"; | |
public static final String requestTokenUrl="https://api.twitter.com/oauth/request_token"; | |
public static final String accessTokenUrl="https://api.twitter.com/oauth/access_token"; | |
public static final String authorizeUrl="https://api.twitter.com/oauth/authorize"; | |
private static final String tweet = "Your tweet here"; | |
CommonsHttpOAuthConsumer consumer; | |
OAuthProvider provider; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
try{ | |
createConsumerProvider(); | |
authorize(); | |
}catch(Exception e){e.printStackTrace();} | |
} | |
//=================================================================== | |
private void createConsumerProvider(){ | |
try{ | |
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); | |
provider = new DefaultOAuthProvider(requestTokenUrl, accessTokenUrl, authorizeUrl); | |
HttpClient client = new DefaultHttpClient(); | |
}catch(Exception e){e.printStackTrace();} | |
} | |
//=================================================================== | |
private void authorize(){ | |
try{ | |
String authUrl = null; | |
boolean tokenRetrieved = false; | |
try{ | |
authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL); | |
tokenRetrieved = true; | |
}catch(Exception e){ | |
authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL); | |
tokenRetrieved = true; | |
} | |
final boolean tempToken = tokenRetrieved; | |
final WebView webview = new WebView(this); | |
webview.getSettings().setJavaScriptEnabled(true); | |
webview.setWebViewClient(new WebViewClient(){ | |
@Override | |
public void onPageStarted(WebView webview, String authUrl, Bitmap favicon){ | |
shouldOverrideUrlLoading(webview, authUrl); | |
} | |
public boolean shouldOverrideUrlLoading(WebView view, final String url){ | |
try{ | |
if (url.startsWith(TwitterShare.this.CALLBACK_URL)){ | |
provider.retrieveAccessToken(consumer,Uri.parse(url).getQueryParameter("oauth_verifier")); | |
Toast.makeText(TwitterShare.this,"Retrieving details, please wait..",Toast.LENGTH_LONG).show(); | |
webview.setVisibility(View.INVISIBLE); | |
webview.stopLoading(); | |
webview.loadUrl("about:blank"); | |
if(tempToken) | |
updateStatus(); | |
else | |
TwitterShare.this.finish(); | |
} | |
}catch(Exception e){e.printStackTrace();} | |
return true; | |
} | |
}); | |
webview.loadUrl(authUrl); | |
this.setContentView(webview); | |
}catch(Exception e){e.printStackTrace();} | |
} | |
//http://dev.bostone.us/2009/07/16/android-oauth-twitter-updates/#awp::2009/07/16/android-oauth-twitter-updates/ | |
//=================================================================== | |
private void updateStatus(){ | |
try{ | |
// create a request that requires authentication | |
HttpPost post = new HttpPost("http://twitter.com/statuses/update.xml"); | |
final List<NameValuePair> nvps = new ArrayList<NameValuePair>(); | |
// 'status' here is the update value you collect from UI | |
nvps.add(new BasicNameValuePair("status", tweet)); | |
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); | |
// set this to avoid 417 error (Expectation Failed) | |
post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false); | |
// sign the request | |
consumer.sign(post); | |
// send the request | |
HttpClient client=new DefaultHttpClient(); | |
final org.apache.http.HttpResponse response = client.execute(post); | |
// response status should be 200 OK | |
int statusCode = response.getStatusLine().getStatusCode(); | |
final String reason = response.getStatusLine().getReasonPhrase(); | |
// release connection | |
response.getEntity().consumeContent(); | |
if (statusCode != 200) { | |
Log.e("TwitterConnector", reason); | |
throw new OAuthNotAuthorizedException(); | |
} | |
this.finish(); | |
}catch(Exception e){e.printStackTrace();} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment