Reputation: 21452
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<NameValuePair> params = new ArrayList<NameValuePair>();
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<NameValuePair> 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<NameValuePair> params = new ArrayList<NameValuePair>();
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
Upvotes: 0
Views: 1058
Reputation: 24114
I think the problem perhaps is setDoOutput()
, I have tested this sample code, you can read my comments
private class APIRequest extends AsyncTask<Void, Void, String> {
@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);
}
}
Upvotes: 1
Reputation: 1476
I am developing an Android app that uses OAuth authentication to authenticate requests to a RESTful web service. I do not know anything about the specific OAuth package you are using and cannot be certain about the cause of the problem you are having. But, based on differences between what you are doing and what I am doing, I see two potential problems:
You may not be using the right token in the HTTP header. In particular, the token you are using may be a refresh token rather than an access token, and you may need to use the access token instead.
You may be inserting the token in the header incorrectly. In my case, I insert the token using:
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
Upvotes: 1