Getting invalid signature error in response when trying to add target using API

+1 vote
asked Jun 7, 2019 by rahulchurhe2 (180 points)
retagged Jun 7, 2019 by rahulchurhe2
private void handleUploadTarget() {
    final Dialog dialog = UiUtil.showProgressDialog(this);
    JSONObject jsonObjectRequest = null;
    try {
        UploadTargetRequest request = new UploadTargetRequest();
        String rawString = ""
                +"active" + TARGET_ACTIVE_STATUS
                + "appKey" + CLOUD_KEY
                + "image" + AppUtil.getBase64(getApplicationContext())
                + "meta" + "hello Bro"
                + "name" + "TestAPI"
                + "size" + TARGET_IMAGE_DEFLAUT_SIZE
                + "timestamp" + CurrentTimestamp()
                + "type" + IMAGE_TARGET_TYPE
                + CLOUD_SECRET;
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(rawString.getBytes(StandardCharsets.UTF_8));
        byte[] digest = md.digest();
        String hex = String.format("%064x", new BigInteger(1, digest));
        
        request.setActive(TARGET_ACTIVE_STATUS);
        request.setAppKey(CLOUD_KEY);
        request.setImage(AppUtil.getBase64(getApplicationContext()));
        request.setMeta("helloBro");
        request.setName("TestAPI");
        request.setSignature(hex);
        request.setSize(TARGET_IMAGE_DEFLAUT_SIZE);
        request.setTimestamp(CurrentTimestamp());
        request.setType(IMAGE_TARGET_TYPE);
        jsonObjectRequest = ParseManager.prepareJsonRequest(request);
    } catch (Exception e) { }
    RequestQueue mRequestQueue = Volley.newRequestQueue(this);
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            BASE_URL_SERVER_END + URL_ADD_NEW_TARGET, jsonObjectRequest,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if (response != null) {
                        UploadTargetResponse uploadTargetResponse = null;
                        try {
                            uploadTargetResponse = (UploadTargetResponse) ParseManager.prepareWebServiceResponseObject(UploadTargetResponse.class, response.toString());
                            handleUploadTargetResponse(uploadTargetResponse);
                        } catch (Exception e) {
                        }
                    }
                    UiUtil.dismissDialog(dialog);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {}
    });
    mRequestQueue.add(jsonObjReq);
}
public static String CurrentTimestamp(){
    Long tsLong = System.currentTimeMillis()/1000;
    String ts = tsLong.toString();
    return ts;
}
 public static String getBase64(Context context){
    Resources r = context.getResources();
    Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.image);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
    byte[] imageBytes = baos.toByteArray();
    String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return imageString;
}

1 Answer

–2 votes
answered Jun 12, 2019 by zhnagjian (11,130 points)
Welcome to EasyAR SDK Q&A, where you can ask questions and receive answers from other members of the community.
...