当前位置: 首页 > 知识库问答 >
问题:

如何使用Rest ASTUED从KeyCloak服务中获取承载令牌

柳联
2023-03-14

使用邮递员,我可以毫无问题地获得令牌。如果单击codejava-okhttp,我会得到以下代码片段:

OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=amc-front-shop-service&client_secret=18hsudf9-0132-4r6d-804f-b134837d0d29");
Request request = new Request.Builder()
  .url("https://kc.services.enderby.com/auth/realms/FE-SHOP/protocol/openid-connect/token")
  .method("POST", body)
  .addHeader("Content-Type", "application/x-www-form-urlencoded")
  .build();
Response response = client.newCall(request).execute();

当我尝试在Rest Assured中对请求建模时,我得到了一个400错误,但不清楚为什么:

 private static RequestSpecification keycloakServiceRequestSpec;
    private static String access_token;

    private void setKeycloakServiceSpecs() {
        keycloakServiceRequestSpec = new RequestSpecBuilder()
                .setContentType(ContentType.URLENC)
                .build();
    }

    @Test
    public String getAccessToken() {

        setKeycloakServiceSpecs();

        String clientId = "18hsudf9-0132-4r6d-804f-b134837d0d29";
        String clientSecret = "amc-front-shop-service";

        Response response =

        given()
                .auth().preemptive().basic(clientId, clientSecret)
                        .contentType("application/x-www-form-urlencoded")
                .formParam("grant_type", "client_credentials")
                .formParam("scope", "openid")
        .when()
                .post("https://kc.services.enderby.com/auth/realms/FE-SHOP/protocol/openid-connect/token").
         then().
                assertThat().statusCode(200).extract().response();

        String json = response.getBody().asString();
        JsonPath jsonPath = new JsonPath(json);

        access_token =  jsonPath.getString("access_token");

        logger.info("Oauth Token:" +  access_token);

        return access_token;

    }

我哪里出了问题很明显吗?是否应该将键/值传递到.body()中?

共有1个答案

彭坚壁
2023-03-14

您正在混合客户端ID/Secret并使用基本Auth。此外,scope似乎对客户端凭据流无效。所以尝试代码:

        String clientSecret = "18hsudf9-0132-4r6d-804f-b134837d0d29";
        String clientId = "amc-front-shop-service";

        Response response =

        given()
                .auth().preemptive()
                .contentType("application/x-www-form-urlencoded")
                .formParam("grant_type", "client_credentials")
                .formParam("client_id", clientId)
                .formParam("client_secret", clientSecret)
 类似资料:
  • 我目前正在使用Postman来生成承载令牌,我正在自动化测试中使用它。现在,我也想使用Java中的REST Assured来自动化承载令牌生成过程。请帮帮我.谢了。

  • 我正在尝试使用邮递员应用程序从keycloak获取访问令牌。我使用的流是身份验证代码流。URI am提供的身份验证是 http://localhost:8080/auth/realms/realm_name/openid-connect/token 即使在keycloak中,我在clients下的Security-admin-console中将有效的重定向URI修改为http://localhos

  • 在Postman中,我可以使用 回调URL Auth URL 客户端ID 我如何使用C#代码做同样的事情?

  • 我有一个asp。net应用程序,带有前端和web api部分。我希望移动应用程序能够使用受保护的web apiendpoint。这意味着我需要登录并在本机移动应用程序中处理身份验证令牌。 现在,登录到asp.net应用程序的唯一方法是通过默认asp.net /Account/Login页面。当我发布到登录终结点时,返回的正文只包含我的应用程序的html。令牌然后由asp.net.存储在cookie

  • 我有一个Spring应用程序,它具有keydeport依赖性。前端发送到我的后端承载令牌,我想使用这个令牌从keydeport获取用户名和他的UUID。 这是我的钥匙斗篷配置。 在这个endpoint中,我得到授权头:

  • 我正在Laravel应用程序中使用twitter API。在我的应用程序中,当用户插入url时,我想获得用户名和个人资料图片。我找了很多,但没找到。对于用户信息,我必须让用户登录到twitter,并与我的应用程序连接以获取访问令牌。我只想使用应用程序授权,这样用户就不需要登录twitter。我发现,通过这个搜索API可以搜索带有承载令牌的推文 有什么API可以让我使用URL获取用户屏幕名称吗? 我