【发布时间】:2021-10-10 21:43:40
【问题描述】:
我刚刚开始学习Spring Boot。 我似乎无法正确获取对 Spotify 的 HTTP 请求。 我想通过客户端信用流向 Spotify API 发送带有给定参数的 POST。
我现在开始工作了,但这不是 Spring:
public static String sendAuthRequest() throws IOException, OAuthProblemException, OAuthSystemException {
String client_id = "MY_ID";
String client_secret = "MY_Secret";
OAuthClientRequest clientReqAccessToken = OAuthClientRequest
.tokenLocation("https://accounts.spotify.com/api/token")
.setGrantType(GrantType.CLIENT_CREDENTIALS).setClientId(client_id).setClientSecret(client_secret)
.buildBodyMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
OAuthAccessTokenResponse oAuthResponse = oAuthClient.accessToken(clientReqAccessToken);
return "Access Token: " + oAuthResponse.getAccessToken() + ", Expires in: " + oAuthResponse.getBody();
}
你们能帮我把它改成 Spring 吗? 我很想用 RestTemplate 尝试一下,但可以弄清楚如何正确添加参数。 Spotify 也需要它是 x-www-form-urlencoded
谢谢!
【问题讨论】:
-
您的意思是创建一个 api post 请求吗?
标签: java spring spring-boot api spotify