m having issues with retrieving the bearer token, this is what I have so far :
private static final String API_KEY = “consumerkey”;
private static final String API_SECRET = “consumerapisecret”;
private static final String CHARSET_UTF_8 = “UTF-8”;
String bearer = "";
try {
// Prepare request
URL url = new URL("https://api.twitter.com/oauth2/token");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// Encode API key and secret
String authString = URLEncoder.encode(API_KEY, CHARSET_UTF_8) + ":" + URLEncoder.encode(API_SECRET, CHARSET_UTF_8);
// Apply Base64 encoding on the encoded string
String authStringBase64 = Base64.encodeToString(authString.getBytes(CHARSET_UTF_8), Base64.NO_WRAP);
// Set headers
conn.setRequestProperty("Authorization", "Basic " + authStringBase64);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
// Set body
conn.setDoOutput(true);
byte[] body = "grant_type=client_credentials".getBytes("UTF-8");
conn.setFixedLengthStreamingMode(body.length);
BufferedOutputStream os = new BufferedOutputStream(conn.getOutputStream());
os.write(body);
os.close();
bearer = conn.getResponseMessage();
bearer = new String(body);
}
catch(Exception ex)
{
ex.getMessage();
ex.getStackTrace();
}
return bearer;
So far its not working even with new consumer keys and secrets, its just not working. The way the response message is it has no error code. How do I fix this? Thanks in advance.