I am trying to authenticate with twitter and display my twitter feed in app using a Volley request. Where i can do this if I assign my String URL with the parameter I want to assign it using the getParam() method. However, if i set the URL without the extra parameter and use the getParam(). It fails with a 401?
public class MainActivity extends AppCompatActivity {
private ArrayList<MyTweet> tweets = new ArrayList<MyTweet>();
private TextView post_reponse_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button post_request_button = findViewById(R.id.test);
post_reponse_text = findViewById(R.id.get_response_data);
post_request_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
postRequest();
}
});
}
private void postRequest() {
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
//String url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=realScreenName";
String url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// post_reponse_text.setText("Post Data : " + response);
post_reponse_text.setText("Twitter Feed");
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject session = jsonArray.getJSONObject(i);
MyTweet tweet = new MyTweet("might need to remove", "this also");
tweet.content = session.getString("text");
tweet.author = session.getString("id_str");
tweets.add(tweet);
}
/*Send to Adapter*/
RecyclerView recyclerView = findViewById(R.id.my_recycler_view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
MyTweetsAdapter Adapter = new MyTweetsAdapter(tweets);
recyclerView.setAdapter(Adapter);
} catch (Exception e) {
e.printStackTrace();
Log.v("Error:", "Error Creating JSON object");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
post_reponse_text.setText("Failed to find Twitter feed");
}
})
{
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap <String, String>();
String name = "realScreenName";
params.put("screen_name", name);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", "OAuth oauth_consumer_key=\"realKey\",oauth_token=\"realKey\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1591115128\",oauth_nonce=\"oEbfgtjeKwA\",oauth_signature=\"realKey\"");
return params;
}
};
queue.add(stringRequest);
}