java - 使用 HttpClient 通过 java 客户端访问 power Bi 组

标签 java powerbi resteasy

我想使用 ResteasyClient 访问 Power Bi 中的组列表,我想使用服务主体(仅应用程序 token )的身份验证形式。
我有应用程序 ID(客户端),应用程序 secret ( key )范围 = Group.Read.All,访问 token URL https://login.microsoftonline.com/common/oauth2/token和授权类型 = 客户端凭据。

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;
    }

谢谢你。

我有一个许可证(powerbi,)我应该能够用用户名和密码生成 token ,但它不起作用。我的回复当前状态为 400,但我的网址似乎是正确的。
基本上我有这个
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

当前响应
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]

最佳答案

通过 http post 请求进行的身份验证握手中存在错误。
收到的结果总是以相同类型的错误结束:

HTTP/1.1 400 Bad Request

我们需要将标题“ Accept ”明确设置为“ None ”:
post.addHeader("Accept", "None");

enter image description here

关于java - 使用 HttpClient 通过 java 客户端访问 power Bi 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57609701/

相关文章:

powerbi - Power BI 中的 SUM IF

powerbi - PowerBI 中的拼写检查

java - Swagger 添加 Controller 的私有(private)字段作为请求的主体参数

powerbi - Power Query - Table.TranformColumns

java - 如何为resteasy实现bson生产者/消费者

glassfish - JAX-RS 自定义路径参数验证器

java - hibernate 迁移:BeanlibException

java - 无法从服务器 android 播放视频

Java Web App上传和加载图像

java - 在这种情况下,我该如何纠正功能嫉妒?