Tuesday 14 August 2018

android - no authentication challenges found Error with Get request



Its first time I am using auth0 to generate token to secure connection with my web service.




I used it for logging - I pass email & password this my url and I get token



this my code , and its work fine



 public String post_to_webservice() {
final StringBuilder stringBuilder = new StringBuilder();
final String[] line = {null};
final BufferedReader[] reader = {null};



new Thread(new Runnable() {
@Override
public void run() {
try {

List params = new ArrayList();
params.add(new BasicNameValuePair("email", "test"));
params.add(new BasicNameValuePair("password", "test"));
URL url = new URL("https://validate.co.nz/api/public/api/authenticate");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");
connection.setReadTimeout(30000);

OutputStream outputStream = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();

outputStream.close();
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
reader[0] = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line[0] = reader[0].readLine()) != null) {
stringBuilder.append(line[0]);
}
}



} catch (Exception e) {
e.printStackTrace();

} finally {
if (reader[0] != null) {
try {
reader[0].close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

runOnUiThread(new Runnable() {
@Override
public void run() {
try {
String message = stringBuilder.toString();
JSONObject object = new JSONObject(message);
Token = (String) object.get("token");

Toast.makeText(MainActivity.this, "token is : " + Token, Toast.LENGTH_LONG).show();

} catch (Exception e) {
e.printStackTrace();

}

}
});


}

}).start();
return stringBuilder.toString();
}


private String getQuery(List params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;


for (NameValuePair pair : params) {
if (first)
first = false;
else
result.append("&");

result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));

}

return result.toString();
}


but I need use the same url but with get request to get data by passing my token
in header



here is my try




 public String get_request() {

final StringBuilder stringBuilder = new StringBuilder();
final String[] line = {null};
final BufferedReader[] reader = {null};
final String[] result = {null};

new Thread(new Runnable() {
@Override

public void run() {
try {
// second try pass token here
List params = new ArrayList();
params.add(new BasicNameValuePair("token", Token));


URL url = new URL("https://validate.co.nz/api/public/api/authenticate");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// I try put my token in header but its fail

String base64Auth = Base64.encodeToString(Token.getBytes(), Base64.NO_WRAP);
connection.setRequestProperty("Authorization", "Basic " + base64Auth);
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
connection.setRequestMethod("GET"); // get request
connection.setReadTimeout(300000);
connection.setDoOutput(true);
connection.setDoInput(true);


OutputStream outputStream = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
outputStream.close();
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
reader[0] = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line[0] = reader[0].readLine()) != null) {

stringBuilder.append(line[0]);
}
result[0] =stringBuilder.toString();
}


} catch (Exception e) {
e.printStackTrace();
result[0] = e.getMessage();


} finally {
if (reader[0] != null) {
try {
reader[0].close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


runOnUiThread(new Runnable() {
@Override
public void run() {

Toast.makeText(MainActivity.this, "Result: " + result[0], Toast.LENGTH_LONG).show();


}
});


}

}).start();
return result[0];

}


I get this error no authentication challenges found
any suggestion thank you



Answer



I think the problem perhaps is setDoOutput(), I have tested this sample code, you can read my comments



private class APIRequest extends AsyncTask {
@Override
protected String doInBackground(Void... params) {
try {
String token = "0123456789";
URL url = new URL("https://validate.co.nz/api/public/api/authenticate");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();

urlConnection.setDoInput(true);
urlConnection.setDoOutput(true); //HERE: if TRUE, receive "No authentication challenges found"; if FALSE, receive "token_not_provided"
urlConnection.setRequestMethod("GET"); // I think if GET, should setDoOutput(false);
urlConnection.setRequestProperty("Authorization", "Basic " + token);
urlConnection.connect();
InputStream inputStream;
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
inputStream = urlConnection.getErrorStream();
} else {
inputStream = urlConnection.getInputStream();

}
return String.valueOf(urlConnection.getResponseCode()) + " " + urlConnection.getResponseMessage();
} catch (Exception e) {
return e.toString();
}
}

@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);

}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...