Application-only authentication problem

android
oauth

#1

I face a problem that any time I make a request in retrofit , the response is always null , I think the problem is in the authentication as the app crashes when I try to get the response code like the code below

    URL url = new URL("https://api.twitter.com/oauth2/token");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    String urlEncodedKey = URLEncoder.encode(twitter_key,"UTF-8");
    String urlEncodedSecret = URLEncoder.encode(twitter_secret,"UTF-8");
    String authorizationStringTobeEncoded = urlEncodedKey + ":" + urlEncodedSecret;
    String encodedAuthorization=base64Encode(authorizationStringTobeEncoded.getBytes());
    connection.setRequestMethod("POST");
    connection.addRequestProperty("Authorization", "Basic "+encodedAuthorization);
    connection.addRequestProperty("grant_type","client_credentials");
    connection.addRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
    connection.addRequestProperty("Content-Length","29");
    connection.addRequestProperty("Accept-Encoding","gzip");