• Java 请求华为云认证接口API


    调用接口有如下两种认证方式,您可以选择其中一种进行认证鉴权。
    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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  • 相关阅读:
    【试题037】逻辑非!例题2
    Mycat2.0搭建教程
    【Golang】简记操作:Centos安装、卸载、升级Golang运行环境
    FreeBASIC通过Delphi7 DLL调用MS SOAP使用VB6 Webservice
    可扩展性对物联网管理系统有哪些影响?
    不用入耳就有好音质,南卡OE Pro 0压开放式耳机
    《IEEE Transactions on Robotics》发表!北京大学研究团队推出具有多种运动模态的软体两栖机器人
    pytorch升级打怪(三)
    2023年度AWS SAP直冲云霄训练营学习分享
    色彩的基础知识——适用于camera tuning
  • 原文地址:https://blog.csdn.net/CXY_BOY/article/details/126193492