代码实现
// This file is auto-generated, don't edit it. Thanks.
package app;
import com.aliyun.tea.*;
import com.aliyun.ocr_api20210707.*;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.ocr_api20210707.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
return new com.aliyun.ocr_api20210707.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.ocr_api20210707.Client client = Sample.createClient("你自己的accessKeyId", "你自己的accessKeySecret");
//创建请求
RecognizeIdcardRequest recognizeIdcardRequest = new RecognizeIdcardRequest();
// url方式
// recognizeIdcardRequest.setUrl("https://img.alicdn.com/tfs/TB1q5IeXAvoK1RjSZFNXXcxMVXa-483-307.jpg");
// 二进制文件流方式
recognizeIdcardRequest.setBody(new BufferedInputStream(new FileInputStream("C:\\Users\\Desktop\\杂七杂八\\傻猫.png")));
// 返回值
RecognizeIdcardResponse recognizeIdcardResponse = client.recognizeIdcard(recognizeIdcardRequest);
System.out.println(recognizeIdcardResponse);
}
}
报错
code: 400, Specified signature does not match our calculation. server StringToSign is…
Code -> SignatureDoesNotMatch…
找了半天原因,查了很多资料也没找到,后来仔细检查发现是accessKeySecret有一位数错了…。