Created
September 15, 2013 22:28
-
-
Save Rockncoder/6574852 to your computer and use it in GitHub Desktop.
Just three steps
This file contains hidden or 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
private String getTwitterStream(String screenName) { | |
String results = null; | |
// Step 1: Encode consumer key and secret | |
try { | |
// URL encode the consumer key and secret | |
String urlApiKey = URLEncoder.encode(CONSUMER_KEY, "UTF-8"); | |
String urlApiSecret = URLEncoder.encode(CONSUMER_SECRET, "UTF-8"); | |
// Concatenate the encoded consumer key, a colon character, and the | |
// encoded consumer secret | |
String combined = urlApiKey + ":" + urlApiSecret; | |
// Base64 encode the string | |
String base64Encoded = Base64.encodeToString(combined.getBytes(), Base64.NO_WRAP); | |
// Step 2: Obtain a bearer token | |
HttpPost httpPost = new HttpPost(TwitterTokenURL); | |
httpPost.setHeader("Authorization", "Basic " + base64Encoded); | |
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); | |
httpPost.setEntity(new StringEntity("grant_type=client_credentials")); | |
String rawAuthorization = getResponseBody(httpPost); | |
Authenticated auth = jsonToAuthenticated(rawAuthorization); | |
// Applications should verify that the value associated with the | |
// token_type key of the returned object is bearer | |
if (auth != null && auth.token_type.equals("bearer")) { | |
// Step 3: Authenticate API requests with bearer token | |
HttpGet httpGet = new HttpGet(TwitterStreamURL + screenName); | |
// construct a normal HTTPS request and include an Authorization | |
// header with the value of Bearer <> | |
httpGet.setHeader("Authorization", "Bearer " + auth.access_token); | |
httpGet.setHeader("Content-Type", "application/json"); | |
// update the results with the body of the response | |
results = getResponseBody(httpGet); | |
} | |
} catch (UnsupportedEncodingException ex) { | |
} catch (IllegalStateException ex1) { | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment