I am trying to make simple app to send text on ChatGPT API, using the API key but I am getting 403 response.
private class SendRequestTask extends AsyncTask<String, Void, String> {
private static final String API_KEY = "**************************************************";
private static final String ENDPOINT_URL = "https://api.openai.com/v1/engines/text-davinci-003/jobs";
@Override
protected String doInBackground(String... params) {
String input = params[0];
String response = "";
try {
URL url = new URL(ENDPOINT_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
connection.setDoOutput(true);
String requestBody = "{\"prompt\": \"" + input + "\"}";
OutputStream os = connection.getOutputStream();
os.write(requestBody.getBytes());
os.flush();
os.close();
int responseCode = connection.getResponseCode();
Log.d("Response Code", String.valueOf(responseCode));
if (responseCode == HttpURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
After flushing the DNS, I've tried cleaning, rebuilding, opening Android studio as an administrator and copy all the code to a new project but with the same result. I've tested in on my Android phone as well and I found no info in the documentation.