2017-04-04 56 views
-1

postman我们可以很容易地得到令牌,并且可以使用another api header作为authorization。如何在休息API中使用放心获得授权令牌?可能吗?如何在休息API中使用放心获得授权令牌?

{ 
    "status_code": 201, 
    "status": "success", 
    "results": { 
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImJpdGh5QGdtYWlsLmNvbSIsImVtYWlsIjoiYml0aHlAZ21haWwuY29tIiwidXNlcl9pZCI6MywiZXhwIjoxNDkxODk2MjkwfQ.obAP1k8ObcyxLTHjQ3fP-9otvQAZNC0wM3TVoBXDEmE", 
    "user_type": "employer" 
    }, 
    "errors": null, 
    "message_code": "sc_vw_user_login_1", 
    "message": "Login successful." 
} 
+0

请采取参观[我如何问一个好问题?](// stackoverflow.com/help/how-to-ask)和[如何创建一个最小,完整和可验证的示例](// stackoverflow.com/help/mcve)。 – Olaia

回答

-1

正如Olaia提到的那样,您需要阅读有关如何提出堆栈溢出问题的文档。

反正回答你的问题,你可以参考下面的步骤

`String response = 
      given() 
       .parameters("username", username, "password", password) 
       .auth() 
       .preemptive() 
      .when() 
       .post("/oauth/token") 
       .asString(); 

    JsonPath jsonPath = new JsonPath(response); 
    accessToken = jsonPath.getString("access_token");` 

然后在后续请求你的情况下传递相同的accessToken在头

given() 
     .auth().oauth2(accessToken) 
     .contentType(ContentType.JSON) 
     .accept(ContentType.JSON) 
    .expect()...