Issue with calling face verify API using two face ids

Devdutta Basu 0 Reputation points
2023-10-02T10:16:07.31+00:00

Hi,

In new java i am not able to use RestTemplate in older java with RestTemplate it worked as normal.

When I am using web client its giving 400 error
Please refer to below code and please help me debug this i am using the headers and body as normal documentation

package aero.sita.sp.spm.dal.dto;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class VerifyFaceRequest {

private String faceId1;
private String faceId2;

public String getFaceId1() {
	return faceId1;
}
public void setFaceId1(String faceId1) {
	this.faceId1 = faceId1;
}
public String getFaceId2() {
	return faceId2;
}
public void setFaceId2(String faceId2) {
	this.faceId2 = faceId2;
}


}

package aero.sita.sp.spm.dal.dto;

public class VerifyFaceResponse {

	private boolean isIdentical  ;
	private float confidence;
	public boolean isIdentical() {
		return isIdentical;
	}
	public void setIdentical(boolean isIdentical) {
		this.isIdentical = isIdentical;
	}
	public float getConfidence() {
		return confidence;
	}
	public void setConfidence(float confidence) {
		this.confidence = confidence;
	}	
}

HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));
		   
				  VerifyFaceRequest request1 = new VerifyFaceRequest();
				  request1.setFaceId1(passportImageFaceId);
				  request1.setFaceId2(liveImageFaceId);

				VerifyFaceResponse response = null;
		
				WebClient webClient = WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
		 	    
		response = webClient.post()
	    		  .uri("https://domain/face/v1.0/verify") 
	    		  .header(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON_VALUE)
			      .header(OCP_APIM_SUBSCRIPTION_KEY, ocpapimsubscriptionkey)
			      .body(Mono.just(request1),DetectFaceRequest.class)
			      .retrieve()
			      .bodyToMono(DetectFaceResponse.class).block();
		
			

Previously this was used which worked:

verifyResponse = restTemplate.exchange(getUtil().getFaceVerifyAPIUri(), HttpMethod.POST, entity, String.class).getBody();
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,443 questions
{count} votes

2 answers

Sort by: Most helpful
  1. YutongTie-MSFT 46,996 Reputation points
    2023-10-03T20:17:51.5933333+00:00

    Hello @
    Devdutta Basu

    Thanks for reaching out to us, I want to let you know that Face API Verify feature is under limited access now, if you have not applied for the access and got approval, you may not be able to use it. Please leverage below link for application.

    Face API: Identify and Verify features

    Document reference, please check here - https://learn.microsoft.com/en-us/azure/ai-services/cognitive-services-limited-access

    If you have already got the access, I may recommend you refer to the official sample for JAVA -

    // // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
    import java.net.URI;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    public class JavaSample 
    {
        public static void main(String[] args) 
        {
            HttpClient httpclient = HttpClients.createDefault();
    
            try
            {
                URIBuilder builder = new URIBuilder("https://westus.api.cognitive.microsoft.com/face/v1.0/verify");
    
    
                URI uri = builder.build();
                HttpPost request = new HttpPost(uri);
                request.setHeader("Content-Type", "application/json");
                request.setHeader("Ocp-Apim-Subscription-Key", "{subscription key}");
    
    
                // Request body
                StringEntity reqEntity = new StringEntity("{body}");
                request.setEntity(reqEntity);
    
                HttpResponse response = httpclient.execute(request);
                HttpEntity entity = response.getEntity();
    
                if (entity != null) 
                {
                    System.out.println(EntityUtils.toString(entity));
                }
            }
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
    
    
    

    Reference document -

    https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f3039523a

    I hope this helps.

    Regards,

    Yutong

    0 comments No comments

  2. Devdutta Basu 0 Reputation points
    2023-10-09T09:36:21.46+00:00

    Hi, sorry it has started working now it was a problem with the account itself. Thanks for your help