private static RefreshTokenInfo getTokenInternal()

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


    private static RefreshTokenInfo getTokenInternal(final String deviceCode, final String clientId) throws IOException {
        QueryParams qp = new QueryParams();
        qp.add("resource", resource);
        qp.add("client_id", clientId);
        qp.add("grant_type", "device_code");
        qp.add("code", deviceCode);
        String bodyString = qp.serialize();

        URL url = new URL(tokenUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.getOutputStream().write(bodyString.getBytes("UTF-8"));

        RefreshTokenInfo token = new RefreshTokenInfo();
        String tokentype = null;
        String scope = null;
        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("token_type")) tokentype = fieldValue;
                        if (fieldName.equals("scope")) scope = fieldValue;
                        if (fieldName.equals("expires_in")) expiryPeriod = Integer.parseInt(fieldValue);
                        if (fieldName.equals("access_token")) token.accessToken = fieldValue;
                        if (fieldName.equals("refresh_token")) token.refreshToken = fieldValue;
                    }
                    jp.nextToken();
                }
                jp.close();

                if (!"Bearer".equals(tokentype) || !"user_impersonation".equals(scope) ) {
                    throw new IOException("not sure what kind of token we got");
                }

                long expiry = System.currentTimeMillis();
                expiry = expiry + expiryPeriod * 1000L; // convert expiryPeriod to milliseconds and add
                token.accessTokenExpiry = new Date(expiry);
                return token;
            } catch (Exception ex) {
                log.debug("Exception retrieving token from AAD response" + ex.toString());
                throw ex;
            } finally {
                httpResponseStream.close();
            }
        } else if (httpResponseCode == 400) {
            InputStream httpResponseStream = conn.getErrorStream();
            try {
                String error = null;

                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("error")) error = fieldValue;
                    }
                    jp.nextToken();
                }
                jp.close();

                if (!"authorization_pending".equals(error)) {
                    String message = "Failed to acquire token from AzureAD. Http response: " + httpResponseCode + " Error: " + error;
                    log.debug(message);
                    throw new IOException(message);
                } else {
                    log.debug("polled AAD for token, got authorization_pending (still waiting for user to complete login)");
                }
            } finally {
                httpResponseStream.close();
            }
        } else {
            String message = "Failed to acquire token from AzureAD. Http response: " + httpResponseCode + " " + conn.getResponseMessage();
            log.debug(message);
            throw new IOException(message);
        }
        return null;
    }