1

I am new on Spotify Web API and I am facing some problems to get the token through the Client Credentials Flow.

I could implement the request at Advanced REST Client with the following configs: The request by Advanced REST Client

To represent this request I implemented the following code:

The Rest Gateway Template:

public final class RestGatewayClient {

    private final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();

    public void addHeader(String key, String value) {
        interceptors.add(new HeaderRequestInterceptor(key, value));
    }

    public static RestTemplate getOAuthTemplate() {
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new HeaderRequestInterceptor(Constants.SPOTIFY_HEADER_AUTH_KEY, Constants.SPOTIFY_HEADER_AUTH_VALUE));
        interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/x-www-form-urlencoded"));

        RestTemplate template = new RestTemplate();
        template.setInterceptors(interceptors);

        return template;
    }

}

The Header Interceptor:

public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {

    private final String headerName;
    private final String headerValue;

    public HeaderRequestInterceptor(String headerName, String headerValue) {
        this.headerName = headerName;
        this.headerValue = headerValue;
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {

        HttpRequest wrapper = new HttpRequestWrapper(request);
        wrapper.getHeaders().set(headerName, headerValue);

        return execution.execute(wrapper, body);
    }

}

The Controller who calls the API:

@ResponseBody
    @RequestMapping(value = "/addTrackIntoPlaylist", method = RequestMethod.GET)
    public SpotifyOAuthTokenResource getAlbumTracks() {

        RestTemplate authTemplate = RestGatewayClient.getOAuthTemplate();
        String uri = Constants.SPOTIFY_API_AUTH__URI;

        MultiValueMap<String, String> payload = new LinkedMultiValueMap<String, String>();
        payload.add(Constants.SPOTIFY_PAYLOAD_GRANT_AUTH_KEY, Constants.SPOTIFY_PAYLOAD_GRANT_AUTH_VALUE);
        payload.add(Constants.SPOTIFY_PAYLOAD_SCOPE_AUTH_KEY, Constants.SPOTIFY_PAYLOAD_SCOPE_AUTH_VALUE);

        ResponseEntity<SpotifyOAuthTokenResource> response = authTemplate.postForEntity(uri, payload, SpotifyOAuthTokenResource.class);

        return response.getBody();
    }

And the resource object that receive the information from the API: The get and set methods are implemented!

@JsonIgnoreProperties(ignoreUnknown = true)
public class SpotifyOAuthTokenResource implements Serializable {

    private static final long serialVersionUID = 1L;

    private String access_token;
    private String token_type;
    private Integer expires_in;
}

But when I call the method in the browser I get the following error: Error Screen

Could anyone help me and find out what I am doing wrong?

Thank you very much!

0 Answers0