问题描述
我必须实现一个 API,该 API 旨在使用 Rest 模板通过 Apple 服务器验证收据。但是,我总是从 https://sandbox.itunes.apple.com/verifyReceipt
收到状态代码 21002 public void verifyReceipt(Purchase purchase) {
String uri = "https://sandBox.itunes.apple.com/verifyReceipt";
String receipt = new String(Base64.getEncoder().encode(purchase.getReceipt().getBytes()));
Map<String,String> params = new HashMap<>();
params.put("receipt-data",receipt);
params.put("password","secret");
Gson gson = new Gson();
String json = gson.toJson(params);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
httpentity<String> request = new httpentity<>(json,headers);
ResponseEntity<String> response = restTemplate.exchange(uri,HttpMethod.POST,request,String.class);
....
}
Apple 文档: https://developer.apple.com/documentation/appstorereceipts/verifyreceipt
有什么想法吗?
解决方法
什么是 Purchase
类?客户端(iOS App)发送到我们服务器端的收据数据应该已经是base64编码了。