gpt4 book ai didi

java - 通过 Java 使用 auth token 发出 api 请求

转载 作者:行者123 更新时间:2023-12-03 10:55:41 24 4
gpt4 key购买 nike

我是软件开发的新手,我正在从事一个项目,该项目使用不记名 token 对交易服务器进行 API 调用。

我已经弄清楚如何通过命令行通过 curl 调用 API。它看起来像这样(它是通过 http 而不是 https,因为我使用的是他们的测试服务器,它只支持 http 请求)

curl http://api.testwebsite.com/transactions/ \
-H "Authorization: Bearer <TOKEN ID>" \
-d amount=500 \
-d currency="USD" \
-d card_number="<card number>" \
-d card_exp_month=<exp month> \
-d card_exp_year=<exp year> \
-d card_cvv="<card's ccv>"

现在我正在用 Java(必须是 Java)编写一个程序,只使用 native Java 库(没有像 Spring 或 Apache 这样的第 3 方包),我正在尝试通过 HttpUrlConnection 调用 API。

import java.io.BufferedReader; 
import java.io.InputStreamReader;
import java.io.DataOutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import java.net.HttpURLConnection;

public class TestHttpsPost {

public static void main(String[] args) throws Exception {

// Uncomment one of these next two lines:
URL url = new URL("http://api.testwebsite.com/transactions");
//URL url = new URL("https://httpbin.org/post"); // good for testing

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);

// Change this to a valid token:
connection.setRequestProperty("Authorization", "Bearer <TOKEN ID>");
/*

connection.setRequestProperty("amount", "500");
connection.setRequestProperty("currency", "USD");
connection.setRequestProperty("card_number", "<CARD_NUMBER>");
connection.setRequestProperty("card_exp_month", "<EXP_MONTH>");
connection.setRequestProperty("card_exp_year", "<EXP_YEAR");
connection.setRequestProperty("card_cvv", "<CCV>");
*/




String jsonData1 = "amount=500";
String jsonData2 = "currency=\"USD\"";
String jsonData3 = "card_number=\"11111111111111111\"";
String jsonData4 = "card_exp_month=11";
String jsonData5 = "card_exp_year=2011";
String jsonData6 = "card_cvv=\"123\"";




try {
// Post the data:
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeBytes(jsonData1);
output.writeBytes(jsonData2);
output.writeBytes(jsonData3);
output.writeBytes(jsonData4);
output.writeBytes(jsonData5);
output.writeBytes(jsonData6);

output.close();

// Read the response:
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}

System.out.println("Response code:" + connection.getResponseCode());
System.out.println("Response message:" + connection.getResponseMessage());
}
}

但我不断得到

Server returned HTTP response code: 400 for URL: http://api.testwebsite.com/transactions
Response code:400
Response message:Bad Request

我关注了Transforming a PayPal curl request to an http request in Java ,但是 paypal api 的字段非常不同,我不确定我是否搞砸了。

提前致谢!

最佳答案

真正好的选择是使用 Retrofit(一种类型安全的 java 和 Android 的 http 客户端)

http://square.github.io/retrofit/

这是使用身份验证 token 的服务示例:

public class ServiceGenerator {
public static final String BASE_URL = "https://your.api.url";
// returns an API client of type serviceClass
public static <S> S createService(
Class<S> serviceClass, final String token) {
RestAdapter.Builder builder = new RestAdapter.Builder() .setEndpoint(BASE_URL)
.setClient(new OkClient(new OkHttpClient()));
if (token != null) {
builder.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) { request.addHeader("Accept", "application/json"); request.addHeader("Authorization", token);
} });
}
RestAdapter adapter = builder.build();
return adapter.create(serviceClass); }
}

关于java - 通过 Java 使用 auth token 发出 api 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37493747/

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