googlehomeステータスレポート

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/qq_44701258/article/details/102752744

 

ステータスレポート:

1トークンJWTを作成します。


   private String getJwt() throws IOException {

        JwtBuilder jwts = Jwts.builder();

        // set claims

        Map claims = new HashMap<>();

        claims.put("exp", System.currentTimeMillis() / 1000 + 3600);

        claims.put("iat", System.currentTimeMillis() / 1000);

        claims.put("iss", "填入包含服务帐号私钥的 JSON 文件中的client_email");

        claims.put("aud", "https://accounts.google.com/o/oauth2/token");

        claims.put("scope", "https://www.googleapis.com/auth/homegraph");



        jwts.setClaims(claims).signWith(SignatureAlgorithm.RS256, serviceAccount.getPrivateKey());

        return jwts.compact();

    }

 

2アクセストークンを取得

 private String getAccessToken(String jwt) throws Exception {

        Map m = new HashMap();

        m.put("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");

        m.put("assertion", jwt);

        // Request parameters and other properties.

        StringEntity params = new StringEntity(formEncode(m));

        HttpEntity entity = httpRequest("https://accounts.google.com/o/oauth2/token", jwt, params, "application/x-www-form-urlencoded");

        if (entity != null) {

            final StringBuilder out = readResponse(entity);

            String res = out.toString();

            if (res.indexOf("access_token") > 0) {

                String token = res.split(":")[1];

                token = token.substring(2, token.indexOf(",") - 1);

                System.out.println("token=" + token);

                return token;

            }

        }

        return "";

    }

 

3つのステータスレポート

  private void callRS(String token, String requestId) throws Exception {

        

       JSONObject json = prepareJson(requestId);



        StringEntity params = new StringEntity(json.toString());



        HttpEntity entity = httpRequest("https://homegraph.googleapis.com/v1/devices:reportStateAndNotification", token, params, "application/json");



        if (entity != null) {

            final StringBuilder out = readResponse(entity);

            log.info("information returned by the report status:" + out.toString());

        }

    }

 

その他:

private HttpEntity httpRequest(String url, String token, StringEntity params, String type) throws IOException {

        HttpClient httpclient = HttpClients.createDefault();

        HttpPost httppost = new HttpPost(url);

        httppost.setHeader("Authorization", "Bearer " + token);



        // Request parameters and other properties.

        httppost.addHeader("content-type", type);

        httppost.setEntity(params);



        // Execute and get the response.

        HttpResponse response = httpclient.execute(httppost);



        HttpEntity entity = response.getEntity();

        return entity;

    }



    private StringBuilder readResponse(HttpEntity entity) throws IOException {

        InputStream instream = entity.getContent();

        final int bufferSize = 1024;

        final char[] buffer = new char[bufferSize];

        final StringBuilder out = new StringBuilder();



        try {

            Reader in = new InputStreamReader(instream, "UTF-8");

            while (true) {

                int rsz = in.read(buffer, 0, buffer.length);

                if (rsz < 0)

                    break;

                out.append(buffer, 0, rsz);

            }

        } finally {

            instream.close();

        }

        return out;

    }



    private String formEncode(Map m) {

        String s = "";

        for (String key : (Set<String>) m.keySet()) {

            if (s.length() > 0) s += "&";

            s += key + "=" + m.get(key);

        }

        return s;

    }

 

 

おすすめ

転載: blog.csdn.net/qq_44701258/article/details/102752744