【发布时间】:2020-03-10 19:03:59
【问题描述】:
我需要使用 RestAssured - Java 创建测试以测试 REST API。为了获得身份验证令牌(OAuth 2.0),我需要从填写如下屏幕的邮递员请求发送。但是,在 java 测试中,我不能使用邮递员。知道获取身份验证令牌应该如何看起来像 java 代码吗?
【问题讨论】:
标签: java integration-testing rest-assured
我需要使用 RestAssured - Java 创建测试以测试 REST API。为了获得身份验证令牌(OAuth 2.0),我需要从填写如下屏幕的邮递员请求发送。但是,在 java 测试中,我不能使用邮递员。知道获取身份验证令牌应该如何看起来像 java 代码吗?
【问题讨论】:
标签: java integration-testing rest-assured
这应该可行 -
public class AuthenticationUtils {
public static String generateOAuth2Token() {
return "Bearer "+RestAssured.given().auth().basic("{YOUR_USERNAME}", "{YOUR_PASSWORD}")
.formParam("client_id", "{YOUR_CLIENT_ID}")
.formParam("client_secret", "{YOUR_SECRET}")
.formParam("grant_type", "client_credentials")
.formParam("{ANY_OTHER_TOKEN_NAME}", "{TOKEN_VALUE}")
.formParam("redirect_uri", "{http://take-me-here-next.com/api/endpoint}")
.when().post("https://{YOUR_TOKEN_URL}/oauth2/v2.0/token")
.then()
.extract().path("access_token");
}
}
用您的替换 {} 中的值
【讨论】:
请试试这个。
使用"client_secret" 和"client_id" 会创建一个bearerToken,然后我们可以使用它进行身份验证。
RestAssured.baseURI = "http://coop.apps.symfonycasts.com/";
RestAssured.basePath = "/token";
// client_secret is different from oauth2 token
Response response = given()
.formParam("client_id","samapp")
.formParam("client_secret","f56f459193cee56817c2a99c205654b7")
.formParam("grant_type", "client_credentials").post("");
//To get token
String bearerToken = response.jsonPath().get("access_token");
System.out.println("The bearer token value is "+bearerToken);
Assert.assertEquals(200, response.getStatusCode(),"Status code mismatches");
【讨论】:
记下/存储从 uaa 服务器获取的客户端 ID 和密码:
然后首先尝试调用 uaa 获取令牌:
Response response =
given()
.header("Authorization",<client id>:<client secret>)
.contentType("application/x-www-form-urlencoded")
.formParam("grant_type","authorization_code") .formParam("redirect_uri",REDIRECT_URL)
.formParam("response_type","code")
.formParam("code", AUTHORIZATION_CODE)
.formParam("client_id", CLIENT_ID)
.formParam("client_secret", CLIENT_SECRET)
.when()
.post(BASE_URI+"/oauth2/token");
然后像下面这样获取访问令牌和令牌类型的信息:
JSONObject jsonObject = new JSONObject(response.getBody().asString());
String accessToken = jsonObject.get("access_token").toString();
String tokenType = jsonObject.get("token_type").toString();
log.info("Oauth Token with type " + tokenType + " " + accessToken);
最后通过在 header 中传递访问令牌来调用您的端点:
Response response =
given()
.headers(
"Authorization",
"Bearer " + accessToken,
"Content-Type",
ContentType.JSON,
"Accept",
ContentType.JSON)
.when()
.get(url)
.then()
.contentType(ContentType.JSON)
.extract()
.response();
【讨论】:
下面是你的代码应该是什么样子的算法/sn-p:
先决条件:
算法:
long validityOfOAuthToken = DB.get ("validityOfOAuthToken");
long OAuthGeneratedTimeInMillis = DB.get ("oAuthGeneratedTime");
long currentTimeInMillis = DateTime.now (DateTimeZone.UTC).getMillis ( );
if (currentTimeInMillis - OAuthGeneratedTimeInMillis > validityOfOAuthToken)
{
String clientID = DB.get ("clientID");
String refreshToken = DB.get ("refreshToken");
String accessToken = generateAccessToken (clientID, refreshToken); // API Invocation to get access token via refresh token
DB.save ("oauthToken", accessToken);
DB.save ("oAuthGeneratedTime", DateTime.now (DateTimeZone.UTC).getMillis ( ));
return accessToken;
}
else
{
return DB.get ("oauthToken");
}
【讨论】: