客户端凭据流向 Spotify API Spring boot
Client Credentials Flows to Spotify API with Spring boot
我是初中生,正在努力学习 Spring 启动。
我似乎无法正确获取对 Spotify 的 HTTP 请求。
我想通过客户端信用流将具有给定参数的 POST 发送到 Spotify API。
我现在可以正常使用了,但这不是 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 服务获得凭据的机制,那么您可以先创建一个 spring 身份验证。可以看一些教程here or a basic here
之后,为客户端或用户创建实体并相应地设置身份验证(例如 User、UserDetail)。然后有选择地考虑委托人身份验证以使用其余控制器检索令牌(在此来自 Spotify)。例如,
@RestController
@RequestMapping("/api/token")
public class TokenController {
@RequestMapping(method = RequestMethod.GET)
public String getToken(@AuthenticationPrincipal UserDetail principal) {
String client_id = principal.getId();
String client_secret = principal.getSecret();
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 启动。 我似乎无法正确获取对 Spotify 的 HTTP 请求。 我想通过客户端信用流将具有给定参数的 POST 发送到 Spotify API。
我现在可以正常使用了,但这不是 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 服务获得凭据的机制,那么您可以先创建一个 spring 身份验证。可以看一些教程here or a basic here
之后,为客户端或用户创建实体并相应地设置身份验证(例如 User、UserDetail)。然后有选择地考虑委托人身份验证以使用其余控制器检索令牌(在此来自 Spotify)。例如,
@RestController
@RequestMapping("/api/token")
public class TokenController {
@RequestMapping(method = RequestMethod.GET)
public String getToken(@AuthenticationPrincipal UserDetail principal) {
String client_id = principal.getId();
String client_secret = principal.getSecret();
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();
}
}