private static DeviceCodeInfo getDeviceCodeInfo()

in src/main/java/com/microsoft/azure/datalake/store/oauth2/DeviceCodeTokenProviderHelper.java [66:118]


    private static DeviceCodeInfo getDeviceCodeInfo(String appId) throws IOException {

        QueryParams qp = new QueryParams();
        qp.add("resource", resource);
        qp.add("client_id", appId);
        String queryString = qp.serialize();

        URL url = new URL(deviceCodeUrl + "?" + queryString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        DeviceCodeInfo dcInfo = new DeviceCodeInfo();
        dcInfo.clientId = appId;
        int httpResponseCode = conn.getResponseCode();
        if (httpResponseCode == 200) {
            InputStream httpResponseStream = conn.getInputStream();
            try {
                int expiryPeriod = 0;

                JsonFactory jf = new JsonFactory();
                JsonParser jp = jf.createParser(httpResponseStream);
                String fieldName, fieldValue;
                jp.nextToken();
                while (jp.hasCurrentToken()) {
                    if (jp.getCurrentToken() == JsonToken.FIELD_NAME) {
                        fieldName = jp.getCurrentName();
                        jp.nextToken();  // field value
                        fieldValue = jp.getText();

                        if (fieldName.equals("user_code")) dcInfo.usercode = fieldValue;
                        if (fieldName.equals("device_code")) dcInfo.devicecode = fieldValue;
                        if (fieldName.equals("verification_url")) dcInfo.verificationUrl = fieldValue;
                        if (fieldName.equals("message")) dcInfo.message = fieldValue;
                        if (fieldName.equals("expires_in")) expiryPeriod = Integer.parseInt(fieldValue);
                        if (fieldName.equals("interval")) dcInfo.pollingInterval = Integer.parseInt(fieldValue);
                    }
                    jp.nextToken();
                }
                jp.close();
                long expiry = System.currentTimeMillis();
                expiry = expiry + expiryPeriod * 1000L; // convert expiryPeriod to milliseconds and add
                dcInfo.expiry = new Date(expiry);
            } finally {
                httpResponseStream.close();
            }
        } else {
            String message = "Failed to get device code from AzureAD. Http response: " + httpResponseCode + " " + conn.getResponseMessage();
            log.debug(message);
            throw new IOException(message);
        }
        log.debug("Obtained device code from AAD: " + dcInfo.usercode);
        return dcInfo;
    }