Hi,
when using DataOutputStream to access Apple server for IAP receipt verification in Custom Business Logic, running on CodeRunner leads to “java.security.AccessControlException: You have no permission to thread manipulation”.
Is there any workaround for this? Or am I missing something?
Java code is as below:
Thank you.
public class IAPVerifier {
private static final String kReceiptValidationKey = "receipt-data"; // JSON key expected by Apple receipt validation server
private static final String kValidationResponseStatusKey = "status"; // key in JSON response that indicates whether receipt is valid
private static final String validationServerURL = "https://sandbox.itunes.apple.com/verifyReceipt"; // Sandbox Server
// private static final String validationServerURL = "https://buy.itunes.apple.com/verifyReceipt"; // Production Server
public static boolean verify(String receipt, String bundle_id, String product_id) throws Exception {
JSONObject validationBodyJSON = new JSONObject();
try {
validationBodyJSON.put(kReceiptValidationKey, receipt);
} catch (JSONException e) {
return false;
}
String validationBodyString = validationBodyJSON.toString();
System.out.println("validating receipt");
HttpURLConnection con = null;
try {
URL obj = new URL(validationServerURL);
con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(validationBodyString);
//add request header
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONObject serverResponseJSON;
try {
serverResponseJSON = new JSONObject(response.toString());
} catch (JSONException e) {
return false;
}
int validationStatus = -1;
try {
if (!serverResponseJSON.isNull(kValidationResponseStatusKey)) {
validationStatus = serverResponseJSON.getInt(kValidationResponseStatusKey);
}
} catch (JSONException e) {
return false;
}
// Take action based on receipt validation
if (validationStatus == 0) {
if (serverResponseJSON.has("bundle_id")) {
if (!serverResponseJSON.getString("bundle_id").equals(bundle_id)) {
return false;
}
} else {
return false;
}
if (serverResponseJSON.has("in_app")) {
JSONArray inAppArray = serverResponseJSON.getJSONArray("in_app");
boolean res = false;
for (int i = 0; i < inAppArray.length(); ++i) {
JSONObject object = inAppArray.getJSONObject(i);
if (object.has("product_id")) {
if (object.getString("product_id").equals(product_id)) {
res = true;
break;
}
}
}
return res;
} else {
return false;
}
} else {
return false;
}
}catch (MalformedURLException e){
throw e;
}catch (ProtocolException e){
throw e;
}catch (IOException e){
throw e;
}finally {
if(con != null) {
con.disconnect();
}
}
}
}