openai-java
openai-java copied to clipboard
retrofit2 HTTP 401 Exception
console print:
Exception in thread "main" retrofit2.adapter.rxjava2.HttpException: HTTP 401 Unauthorized at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:57) at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:38) at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:48) at io.reactivex.Observable.subscribe(Observable.java:12284) at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:35) at io.reactivex.Observable.subscribe(Observable.java:12284) at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35) at io.reactivex.Single.subscribe(Single.java:3666) at io.reactivex.Single.blockingGet(Single.java:2869) at com.theokanning.openai.OpenAiService.createCompletion(OpenAiService.java:91)
my token is fresh, I don't know why I reported this error?
my gradle:
plugins { id 'java' }
group 'org.openai' version '1.0-SNAPSHOT'
repositories { mavenCentral() }
dependencies { implementation 'com.theokanning.openai-gpt3-java:client:0.8.1' implementation group: "org.slf4j", name: "slf4j-simple", version: "1.7.32" testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' }
test { useJUnitPlatform() }
Same here in Kotlin.
implementation 'com.theokanning.openai-gpt3-java:client:0.8.1'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.1'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
Not sure if this will help, but I put mine in env vars (.bashrc)
export OPENAI_TOKEN="sk-K..." export OPENAI_API_KEY="sk-K..."
Downgrading to 0.8.1 and adding OPENAI_TOKEN to env var worked for me.
set the environ. vars. is OK when you are sharing the src code and therefore you dont want to expose your key. But for running locally just type the plain string and avoid the cumbersome: String token = System.getenv("OPENAI_TOKEN"). On the other hand, dont forget to install the lombok.jar when using eclipse IDE.
401 How to solve