调用接口有如下两种认证方式,您可以选择其中一种进行认证鉴权。
Token认证:通过Token认证通用请求。
AK/SK认证:通过AK(Access Key ID)/SK(Secret Access Key)加密调用请求。
1.先获取认证接口的认证鉴权(Token认证方式)
//获取请求链接中用户的token信息
public String getToken() throws IOException {
String tokenStr = "";
//先判断redis中是否有
String value = cacheManager.getValue(Constants.CacheModule.WULIU, "x-subject-token");
if (value!=null){
return value;
}
String iam = "https://iam."+endpoint+"/v3/auth/tokens";
String param = "{\"auth\":{\"identity\":{\"methods\":[\"password\"],\"password\":{\"user\":{\"name\":\"" + username + "\",\"password\":\"" + password + "\",\"domain\":{\"name\":\"" + domainName + "\"}}}},\"scope\":{\"project\":{\"name\":\"cn-north-4\"}}}}";
OutputStreamWriter out;
BufferedReader in = null;
String response = "";
try {
//需要请求的url
URL url = new URL(iam);
//打开和URL之间的连接
URLConnection connection = url.openConnection();
//设置通用的请求属性,请求头部分
//设置Content-type 为 application/x-www-form-urlencoded
connection.addRequestProperty("Content-type", "application/json");
// 发送POST请求必须设置如下两行
connection.setDoInput(true);
connection.setDoOutput(true);
// 建立实际的连接
connection.connect();
// 获取URLConnection对象对应的输出流
out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
// 发送请求参数
out.write(param);
// flush输出流的缓冲
out.flush();
//获取相应头中的token信息
tokenStr = connection.getHeaderField("x-subject-token");
if (tokenStr.isEmpty()){
return null;
}
//将token放在缓存中 避免多次请求
cacheManager.setValue(com.ias.assembly.redis.common.Constants.CacheModule.WULIU, "x-subject-token",token,12*60*60);
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
//打印出相应头中的信息
System.out.println(key + "--->" + map.get(key));
}
}catch (Exception e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
}finally{
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return tokenStr;
}
2.请求认证接口
//司机道路运输许可证识别
public JSONObject transportationLicense(String picUrl,String token) throws IOException{
String ocr = "https://ocr."+endpoint+"/v2/"+projectId+"ocr/transportation-license";
System.out.println("华为云OCR请求地址:"+ocr);
String response = "";
BufferedReader in = null;
String param = "{\"url\":\"" + picUrl + "\"}";
try {
URL url = new URL(ocr);
URLConnection connection = url.openConnection();
//容易载跟头,表明请求体的部分为json形式
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("X-Auth-Token", token);
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
// 获取URLConnection对象对应的输出流
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
// 发送请求参数
out.write(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
String line;
while ((line = in.readLine()) != null) {
response += line;
}
} catch (Exception e) {
System.out.println("POST发送请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
log.info("图片解析结果:"+response);
//返回相应体中的结果,打印出来
JSONObject jsonObject = JSONObject.parseObject(response)
log.info("图片解析结果:"+jsonObject)
return jsonObject;
}