gpt4 book ai didi

java - 使用 Chat GPT API 的 Android Studio 项目返回 403 错误

转载 作者:行者123 更新时间:2023-12-02 05:47:24 26 4
gpt4 key购买 nike

<分区>

我正在尝试制作简单的应用程序以使用 API key 在 ChatGPT API 上发送文本,但我收到 403 响应。

private class SendRequestTask extends AsyncTask<String, Void, String> {

private static final String API_KEY = "**************************************************";
private static final String ENDPOINT_URL = "https://api.openai.com/v1/engines/text-davinci-003/jobs";

@Override
protected String doInBackground(String... params) {
String input = params[0];
String response = "";

try {
URL url = new URL(ENDPOINT_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
connection.setDoOutput(true);

String requestBody = "{\"prompt\": \"" + input + "\"}";
OutputStream os = connection.getOutputStream();
os.write(requestBody.getBytes());
os.flush();
os.close();

int responseCode = connection.getResponseCode();
Log.d("Response Code", String.valueOf(responseCode));

if (responseCode == HttpURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}

刷新 DNS 后,我尝试清理、重建、以管理员身份打开 Android Studio,并将所有代码复制到新项目,但结果相同。我也在我的 Android 手机上进行了测试,但我在文档中找不到任何信息。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com