Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access power Bi groups with java client using HttpClient

I want use ResteasyClient to access list of groups in power Bi, The form of authentication i want to use a Service Principal (app-only token). I have the appllicattion Id (Client), Application Secret (Key) scope = Group.Read.All, Access Token URL https://login.microsoftonline.com/common/oauth2/token and a grant Type = client credentials.

public static String getAccessToken(OAuth2Details oauthDetails) {
    HttpPost post = new HttpPost(oauthDetails.getAuthenticationServerUrl());
        String clientId = oauthDetails.getClientId();
        String clientSecret = oauthDetails.getClientSecret();
        String scope = oauthDetails.getScope();

        List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>();
        parametersBody.add(new BasicNameValuePair(OAuthConstants.GRANT_TYPE,
                oauthDetails.getGrantType()));

        parametersBody.add(new BasicNameValuePair(OAuthConstants.CLIENT_ID,
                clientId));

        parametersBody.add(new BasicNameValuePair(
                OAuthConstants.CLIENT_SECRET, clientSecret));

        if (isValid(scope)) {
            parametersBody.add(new BasicNameValuePair(OAuthConstants.SCOPE,
                    scope));
        }

        HttpClient client = new DefaultHttpClient();
        HttpResponse response = null;
        String accessToken = null;
        try {
            post.setEntity(new UrlEncodedFormEntity(parametersBody, HTTP.UTF_8));
            response = client.execute(post);
            int code = response.getStatusLine().getStatusCode();
            if (code == OAuthConstants.HTTP_UNAUTHORIZED) {
                if (log.isDebugEnabled()) {
                    log.debug("Authorization server expects Basic authentication");
                }
                // Add Basic Authorization header
                post.addHeader(
                        OAuthConstants.AUTHORIZATION,
                        getBasicAuthorizationHeader(oauthDetails.getClientId(),
                                oauthDetails.getClientSecret()));
                if (log.isDebugEnabled()) {
                    log.debug("Retry with client credentials");
                }
                post.releaseConnection();
                response = client.execute(post);
                code = response.getStatusLine().getStatusCode();
                if (code == 401 || code == 403) {
                    if (log.isDebugEnabled()) {
                        log.debug("Could not authenticate using client credentials.");
                    }
                    throw new RuntimeException(
                            "Could not retrieve access token for client: "
                                    + oauthDetails.getClientId());
                }
            }
            Map<String, String> map = handleResponse(response);
            accessToken = map.get(OAuthConstants.ACCESS_TOKEN);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return accessToken;
    }

Thank you.

I have a license (powerbi,)I should be able to generate token with username and password but it doesnt work. My response is currently status 400, But my url seems to be correct. Basically i have this

client_id=affxxx
client_secret=cxxx
username=xxx
password=xxx
authentication_server_url=https://login.microsoftonline.com/common/oauth2/token
grant_type=client_credentials
client_credentials=client_credentials

Current response

HTTP/1.1 400 Bad Request [Cache-Control: no-cache, no-store, Pragma: no-cache, Content-Type: application/json; charset=utf-8, Expires: -1, Strict-Transport-Security: max-age=31536000; includeSubDomains, X-Content-Type-Options: nosniff, x-ms-request-id: 11cd7b41-eaf6-49d4-b6a6-3b19a5569c00, x-ms-ests-server: 2.1.9288.13 - AMS1 ProdSlices, P3P: CP="DSP CUR OTPi IND OTRi ONL FIN", Set-Cookie: fpc=AlrZG0Zj8XhGpMfGBgQKR1Y; expires=Wed, 25-Sep-2019 13:03:32 GMT; path=/; secure; HttpOnly, Set-Cookie: x-ms-gateway-slice=prod; path=/; secure; HttpOnly, Set-Cookie: stsservicecookie=ests; path=/; secure; HttpOnly, Date: Mon, 26 Aug 2019 13:03:32 GMT, Content-Length: 468]
like image 837
valik Avatar asked Aug 22 '19 12:08

valik


1 Answers

There is a bug in the authentication handshakes through http post requests.
The result recieved would always end in the same type of errors:

HTTP/1.1 400 Bad Request

We need to set the header "Accept" to "None" explicitly:

post.addHeader("Accept", "None");

enter image description here

like image 91
GingerHead Avatar answered Oct 20 '22 06:10

GingerHead