I'm working on an android application and I am trying to retrieve JSON objects from a web API. I am able to read public endpoints and display the results. However, when I try to read private endpoints, I get an error "Permission Denied" in the console. I have read the API document and it states to put the api key in the header. It gives an example:
# Example with curl
curl -X GET https://wger.de/api/v2/workout/ \
-H 'Authorization: Token ae45ad5c68667c4f3a5bfcd983b629875732ecbb'
This is my post request class:
public void sendPostRequest (String where) {
URL loc = null;
HttpURLConnection conn = null;
InputStreamReader is;
BufferedReader in;
try {
loc = new URL(where);
}
catch (MalformedURLException ex) {
return;
}
try {
conn = (HttpURLConnection)loc.openConnection();
conn.addRequestProperty("Authorization","my-api-key");
is = new InputStreamReader (conn.getInputStream(), "UTF-8");
in = new BufferedReader (is);
readResponse (in);
}
catch (IOException ex) {
}
finally {
conn.disconnect();
}
}
I tried to add conn.addRequestProperty("Authorization","my-api-key"); but my application fails when trying to recive the JSON object.