0

I am trying to call the chat gpt api (image generation) using the ASYNC Http client> in Android studio I am trying to generate an image based on text input. However i still get a failure whenever i try to call the api. I am still unsure how to go about this and help would be appreciated.

 fun getaiimage(prompt:String) {
            val client = AsyncHttpClient()
            val params = RequestParams()

            //apikey
            val apikey = "API_KEY"

            //Api parameters
            params["prompt"] = prompt
            params["size"] ="256x256"
            params.put("n",1)
            params["response_format"] = "https://api.openai.com/v1/images/generations"
            params["model"] = "image-beta-003"
            params["Authorization"] = "Bearer $apikey"

            client["https://api.openai.com/v1/images/generations",params, object :
                JsonHttpResponseHandler() {
                override fun onFailure(
                    statusCode: Int,
                    headers: Headers?,
                    response: String?,
                    throwable: Throwable?
                ) {
                    Log.d("Image Failed","Big Errors")
                }

                override fun onSuccess(statusCode: Int, headers: Headers?, response: JSONObject) {
                        Log.d("Image Received","No Errors")


                    }
            }]
        }

I tried reformatting the code several times to no avail

cutiko
  • 9,887
  • 3
  • 45
  • 59
Shane
  • 1
  • `params["Authorization"] = "Bearer $apikey"` Shouldn't this be a header? "All API requests should include your API key in an Authorization HTTP header as follows:" https://platform.openai.com/docs/api-reference – Ken Wolf Mar 30 '23 at 16:57
  • That's the thing this library doesn't exactly have a "headers" class. Maybe I should in the "onsuccess" method? – Shane Mar 30 '23 at 17:07
  • You need to be able to set the headers on your request. I would use OkHttpClient and Retrofit: https://square.github.io/retrofit/ – Ken Wolf Mar 30 '23 at 17:08
  • Looks like it does support setting headers - see the advanced example here: https://github.com/codepath/CPAsyncHttpClient/blob/master/example/src/main/java/com/codepath/example/TestActivity.java. Specifically the `RequestHeaders` class – Ken Wolf Mar 30 '23 at 17:10
  • If that was your real API Key you need to reset it in your account. – cutiko Mar 30 '23 at 17:51
  • Can you add the error message you are getting? That will help others answer the question. – kivk02 Mar 31 '23 at 14:56

0 Answers0