Endpoint fails to initialize API-key given by Java "curl"-Runtime processing, error: no API_KEY provided

package org.example;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.io.*;

public class exampleclass extends ListenerAdapter
    public void onMessageReceived(MessageReceivedEvent event)
        MessageChannel channel = event.getChannel();

        // Check if the message was sent in the desired channel
        if (channel.getId().equals("987082455435591680") && !event.getAuthor().isBot())
            Message message = event.getMessage();
            String content = message.getContentRaw();
// API KEY here is provided, but not shown
            String command = "curl https://api.openai.com/v1/completions \\ -H \"Content-Type: application/json\" \\ -H \"Authorization: Bearer API-KEY\" \\ -d '{\"model\": \"text-davinci-003\", \"prompt\": \"" + content + "\", \"temperature\": 0, \"max_tokens\": 1024}'";
                // Send the message to the terminal
                Process process = Runtime.getRuntime().exec(command);

                StringBuilder sb = new StringBuilder();
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

                String line;
                while ((line = reader.readLine()) != null) {
                String output = sb.toString();

                Gson gson = new Gson();
                JsonObject responseJson = gson.fromJson(output, JsonObject.class);
                String responseText = responseJson.get("choices").getAsJsonArray().get(0).getAsJsonObject().get("text").getAsString();

                int exitCode = process.waitFor();
                System.out.println("\nExited with error code : " + exitCode);
                // Send the output as a message in the channel
            catch (IOException e)
            catch (InterruptedException e)
                throw new RuntimeException(e);

this is code partially created by ChatGPT,

it takes the the content of a message in discord (variable: content) in to the String variable “command”, then it executes the command in the “RunTime.getRuntime().exec(command);” method is taking the hole output into a single string and outputs it into discord.

The Endpoint gives me the error, that there is no API-Key provided, but there is.

And yes, i tested the command in the terminal.

If integrating the AI into a Discord Application is against the Terms, which i havent found, then pls tell me

I assume you replaced this with your actual API key?

yes of course, as the website for the api-references says to not show off the key publicly etc.

1 Like