我需要WS
在开始游戏时进行请愿,以便我可以登录外部服务来获取令牌。我需要那个令牌来进行未来的请愿。我知道如何进行 WS 请愿,我不知道在哪里放置代码以在开始时执行。此时,它处于控制器的功能中。
如果你想要一些这样的代码:
// login data
ObjectNode tvdbaccount = Json.newObject();
tvdbaccount.put("apikey", "*****");
tvdbaccount.put("username", "*****");
tvdbaccount.put("userkey", "*****");
// try to login
String token = "";
CompletionStage<JsonNode> request = WS.url("https://api.thetvdb.com/login")
.post(tvdbaccount)
.thenApply(WSResponse::asJson);
try {
JsonNode response = request.toCompletableFuture()
.get(5, TimeUnit.SECONDS);
token = response.get("token").asText();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
该令牌在 24 小时后过期,因此我想例如每 12 小时调用一个函数来刷新该令牌。那个功能和上一个类似,只是一个WS请愿。
我正在使用playframework 2.5
where GlobalSettings
is deprecated 并且我看到 2.5 的多个答案不是很清楚,所以我无法完成它。