The OpenAI API uses API keys for authentication. Visit your API Keys page to retrieve the API key you’ll use in your requests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.io.BufferedReader import java.io.InputStreamReader import java.net.HttpURLConnection import java.net.URL fun main() { val apiKey = "sk-code" val url = URL("https://api.openai.com/v1/models") val connection = url.openConnection() as HttpURLConnection connection.requestMethod = "GET" connection.setRequestProperty("Authorization", "Bearer $apiKey") val responseCode = connection.responseCode if (responseCode == HttpURLConnection.HTTP_OK) { val inputStream = BufferedReader(InputStreamReader(connection.inputStream)) val response = StringBuilder() var inputLine: String? while (inputStream.readLine().also { inputLine = it } != null) { response.append(inputLine) } inputStream.close() val jsonResponse = response.toString() println("Respuesta: $jsonResponse") } else { println("Error al realizar la solicitud: $responseCode") } connection.disconnect() } |