英文:
You didn't pass an authentication token in java
问题
我想使用POST方法。我有一个API,我想使用HttpConnection进行POST请求。但是它不起作用。它显示 "您没有传递身份验证令牌。了解如何进行身份验证并获取您的API令牌:https://replicate.com/docs/reference/http#authentication"
我的代码是
try {
String apiUrl = "https://api.replicate.com/v1/predictions";
String apiToken = "****************";
// 构造JSON负载
JSONObject json = new JSONObject();
json.put("version", "494ca4d578293b4b93945115601b6a38190519da18467556ca223d219c3af9f9");
JSONObject jsonObj = new JSONObject();
File file = new File("C:\\Users\\ruhul\\OneDrive\\ছবিগুলি\\ads.PNG");
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
jsonObj.put("model", "Image Denoising");
jsonObj.put("image", fileBody);
json.put("input", jsonObj);
String message = json.toString();
// 创建HTTP连接
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Token " + apiToken);
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
// 发送JSON负载
OutputStream outputStream = connection.getOutputStream();
outputStream.write(message.getBytes());
outputStream.flush();
outputStream.close();
// 获取响应
int responseCode = connection.getResponseCode();
System.out.println(responseCode);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 打印响应
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
我现在可以做什么?
英文:
I am wanting to use the POST method. I have an API where I want POST using HttpConnection. But It not working. It shows "You didn't pass an authentication token. Learn how to authenticate and get your API token here: https://replicate.com/docs/reference/http#authentication"
My code is
try {
String apiUrl = "https://api.replicate.com/v1/predictions";
String apiToken = "****************";
// Construct the JSON payload
JSONObject json = new JSONObject();
json.put("version", "494ca4d578293b4b93945115601b6a38190519da18467556ca223d219c3af9f9");
JSONObject jsonObj = new JSONObject();
File file = new File("C:\\Users\\ruhul\\OneDrive\\ছবিগুলি\\ads.PNG");
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
jsonObj.put("model", "Image Denoising");
jsonObj.put("image", fileBody);
json.put("input", jsonObj);
String message = json.toString();
// Create the HTTP connection
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Token " + apiToken);
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
// Send the JSON payload
OutputStream outputStream = connection.getOutputStream();
outputStream.write(message.getBytes());
outputStream.flush();
outputStream.close();
// Get the response
int responseCode = connection.getResponseCode();
System.out.println(responseCode);
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// Print the response
System.out.println(response.toString());
}catch (Exception e) {
e.printStackTrace();
}
What I can do now?
答案1
得分: 1
实际上,我认为Java代码本身没有问题。我不知道为什么你会收到身份验证错误,对我来说是422 '不可处理'(我使用了硬盘上的一些随机图片),这意味着JSON输入存在问题。
在你发布的文档中,第一个示例是发送 text: Alicia
作为输入,当你将代码更改为包含此文本而不是文件内容时,它会返回201。
也许首先尝试使用curl验证它是否有效,然后再编写代码,但总的来说,打开连接并发出HTTP请求的代码是有效的。
英文:
Actually, I think there's nothing wrong with the Java code itself. I don't know why you're getting the authentication error, for me it's 422 'unprocessable' (I used some random picture I had on HDD), meaning there's some problems with JSON input.
In the documentation you posted the first example is to send text: Alicia
as the input, and when you change your code to contain this text instead of file contents, it works with 201 returned.
Maybe try and go the curl route first to make sure it works and then code it, but generally, the code to open connection and make a http request works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论