openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

retrofit2 HTTP 401 Exception

Open Jasondeepny opened this issue 2 years ago • 6 comments

截屏2022-12-06 11 09 22

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?

Jasondeepny avatar Dec 06 '22 03:12 Jasondeepny

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() }

Jasondeepny avatar Dec 06 '22 03:12 Jasondeepny

image

image

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'

YaminMahdi avatar Dec 09 '22 14:12 YaminMahdi

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..."

cryptoapebot avatar Dec 11 '22 18:12 cryptoapebot

Downgrading to 0.8.1 and adding OPENAI_TOKEN to env var worked for me.

zkarpinski avatar Jan 13 '23 03:01 zkarpinski

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.

pepitoscrespo avatar Jan 28 '23 11:01 pepitoscrespo

401 How to solve

lin666999 avatar Feb 15 '23 01:02 lin666999