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

获取访问令牌的放心api的java代码应该是什么

乐宜民
2023-03-14

我想从给定的REST-API调用中获取访问令牌。我已经在postman中对此进行了测试,它工作正常,需要在所有3个选项卡中输入数据(授权、标题和正文以及需要启动post方法)。为了更清晰,请查看随附的屏幕截图。请指导我如何使用java和jayaway restassured library或任何其他解决方案自动完成此操作。

邮递员截图-授权选项卡

邮递员截图-标题选项卡

邮递员截图-身体标签

注意:用户名和密码在授权和正文选项卡中不同

共有2个答案

贲言
2023-03-14

假设你的回答是这样的:

{"token_type":"bearer","access_token":"AAAA%2FAAA%3DAAAAAAAA"}

您可以尝试以下放心的示例:

JsonPath jsonPath = RestAssured.given()
    .auth().preemptive().basic("username", "password")
    .contentType("application/x-www-form-urlencoded")
    .formParam("username", "johndoe")
    .formParam("password", "12345678")
    .formParam("grant_type", "password")
    .formParam("scope", "open_d")
    .when()
    .post("http://www.example.com/oauth2/token")
    .then()
    .statusCode(200)
    .contentType("application/json")
    .extract().jsonPath();

String tokenType = jsonPath.getString("token_type");
String accessToken = jsonPath.getString("access_token");
张唯
2023-03-14
RestAssured.baseURI = "http://URI";
Response res = given().header("Content-Type", "application/json")
                .body("{" + "   \"username\":\"yourmail@something.com\"," + "   \"password\":\"ab@1234\""
                        + "}")
                .when().post("/api/token").then().log().all().assertThat().statusCode(200)
                .contentType(ContentType.JSON).extract().response();
String responseString = res.asString();
System.out.println(responseString);
JsonPath js = new JsonPath(responseString);
String str = js.get("data.access_token");
System.out.println(str);
 类似资料:
  • 我需要使用restAssuret-java创建测试来测试REST API。为了获得一个身份验证令牌(OAuth2.0),我需要发送来自邮递员的请求,如下面的屏幕所示。但是,在java测试中,我不能使用Postman。你知道获取身份验证令牌的java代码应该是什么样子吗?

  • 编辑:C#请求示例 编辑:我获得令牌的方式

  • 问题内容: 在我的应用程序中,当用户成功登录时,我将返回访问令牌和刷新令牌。访问和刷新令牌的到期时间已分别设置为10分钟和40分钟。(我应该对这些值进行更多研究。这只是为了测试) 我使用了以下文章中描述的实现 http://www.svlada.com/jwt-token-authentication-with-spring- boot/ 假设我在登录10分钟后向服务器调用了一个请求。由于访问令牌

  • 在我的应用程序中,当用户成功登录时,我返回访问令牌和刷新令牌。访问令牌和刷新令牌的过期时间已分别设置为 10 分钟和 40 分钟。(我应该对这些价值观做更多的研究。这只是为了测试) 我使用了以下文章中描述的实现 http://www.svlada.com/jwt-token-authentication-with-spring-boot/ 假设我在登录10分钟后调用了对服务器的请求。由于访问令牌已

  • 我正在尝试使用以下命令访问 spotify 令牌 生成的网址< code > https://accounts . Spotify . com/authorize?client_id=e...0 查询返回值应为:access_token;标记类型;expires_in和state但我得到了一些HTML作为响应

  • 我正在开发桌面应用程序使用谷歌驱动器API。我用的是C#。在登录阶段,在登录并允许访问后,我收到授权码,要求我复制粘贴到我的应用程序中,以交换授权码来访问令牌。是否有另一种方法来接收访问令牌,而不需要用户复制和粘贴授权代码。 提前感谢莫兰