Reputation: 309
Can anyone show me how to use the application-only oauth in twitter? I have to implement it in my app but i cant find ANY tutorials on net for the new api 1.1. An example would be much better...
Upvotes: 1
Views: 843
Reputation: 8995
Hi the following code include how to use application-only OAuth and get user_timeline.
use this code
/*
* -url user_timeline
* -client and asyncHttpResponseHandler is used to get timeline
*/
String url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=<NAME>";
AsyncHttpClient client = new AsyncHttpClient();
AsyncHttpResponseHandler asyncHttpResponseHandler = new AsyncHttpResponseHandler() {
public void onSuccess(String response) {
/*--------- DidReceiveData ---------*/
Log.e("", "JSON FILE "+" response " + response);
};
};
/*
* OAuth Starts Here
*/
RequestParams requestParams = new RequestParams();
requestParams.put("grant_type", "client_credentials");
AsyncHttpClient httpClient = new AsyncHttpClient();
httpClient.addHeader("Authorization", "Basic " + Base64.encodeToString((CONSUMER_KEY + ":" + CONSUMER_SECRET).getBytes(), Base64.NO_WRAP));
httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
httpClient.post("https://api.twitter.com/oauth2/token", requestParams, new AsyncHttpResponseHandler() {
public void onSuccess(String responce) {
try {
JSONObject jsonObject = new JSONObject(
responce);
Log.e("", "token_type " + jsonObject.getString("token_type") + " access_token " + jsonObject.getString("access_token"));
client.addHeader("Authorization", jsonObject.getString("token_type") + " " + jsonObject.getString("access_token"));
client.get(url, asyncHttpResponseHandler);
} catch (JSONException e) {
e.printStackTrace();
}
};
public void onFailure(Throwable error, String response) {
Log.e("", "error " + error.toString() + " response " + response);
};
});
Upvotes: 1