Stack Overflow en español Asked by WALIXGAM FOWX on February 17, 2021
Estoy intentando usar la API de Spotify para añadir canciones a una playlist pero no se como tengo que usar el accestoken. En otras APIs se incluye en la URI pero aquí mirando la documentación veo que no.
En Facebook por ejemplo sería así:
package es.us.eii.server.resources;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
import es.us.eii.shared.domain.facebook.FacebookFriends;
public class FacebookFriendsResource {
private String uri = "https://graph.facebook.com/me/friends";
private String access_token = null;
public FacebookFriendsResource(String access_token) {
this.access_token = access_token;
}
/**
*
* @return FacebookFriends
*/
public FacebookFriends getFriends() {
ClientResource cr = null;
FacebookFriends list = null;
try {
cr = new ClientResource(uri + "?access_token=" + access_token);
list = cr.get(FacebookFriends.class);
} catch (ResourceException re) {
System.err.println("Error when retrieving friends: " + cr.getResponse().getStatus());
System.err.println(uri + "?access_token" + access_token);
}
return list;
}
}
Spotify:
package aiss.model.resources;
import java.util.logging.Logger;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
import com.google.appengine.repackaged.com.google.protobuf.Api;
import aiss.model.spotify.Playlists;
import aiss.model.spotify.Tracks;
public class SpotifyResource {
private static final Logger log=Logger.getLogger(SpotifyResource.class.getName());
private String uriTrack = " https://api.spotify.com/v1/tracks/";
private String uriPlaylist = "https://api.spotify.com/v1/users/";
// METODO PARA OBTENER UN TRACK
public Tracks getTracks(String trackId) {
ClientResource cr = null;
Tracks track = null;
try {
cr = new ClientResource(uriTrack + trackId);
track = cr.get(Tracks.class);
} catch (ResourceException re) {
System.out.println("Error al recuperar la busqueda:" + cr.getResponse().getStatus());
throw re;
}
return track;
}
//METODO PARA OBTENER LA LISTA DE PLAYLIST DE UN USUARIO
public Playlists getPlaylist(String userId) {
ClientResource cr = null;
Playlists playlist = null;
try {
cr = new ClientResource(uriPlaylist + userId + "/playlists");
playlist = cr.get(Playlists.class);
} catch (ResourceException re) {
System.err.println("Error when adding the playlist: " + cr.getResponse().getStatus());
}
return playlist;
}
// METODO PARA CREAR UNA PLAYLIST PARA UN USUARIO
public Playlists addPlaylist( Playlists playlist, String userId) {
ClientResource cr = null;
Playlists newPlaylist = null;
try {
cr = new ClientResource(uriPlaylist + userId + "/playlists");
cr.setEntityBuffering(true);
newPlaylist=cr.post(playlist,Playlists.class);
} catch (ResourceException re) {
System.out.println("Error al recuperar la busqueda:" + cr.getResponse().getStatus());
throw re;
}
return newPlaylist;
}
// METODO PARA ELIMINAR UNA PLAYLIST
public boolean removePlaylist(String playlistId,String userId) {
ClientResource cr = null;
boolean success = true;
try {
cr = new ClientResource(uriPlaylist + userId+"/playlists/"+playlistId+"/tracks");
cr.setEntityBuffering(true);
cr.delete();
} catch (ResourceException re) {
System.out.println("Error al recuperar la busqueda:" + cr.getResponse().getStatus());
success = false;
}
return success;
}
// METODOS PARA METER UN TRACK EN UNA PLAYLIST
public boolean addTrackToPlaylist(String playlistId, String userId) {
ClientResource cr = null;
boolean success = true;
try {
cr = new ClientResource(uriPlaylist + userId+ "/playlists/"+playlistId+"/tracks");
cr.setEntityBuffering(true);
cr.post(" ");
} catch (ResourceException re) {
System.out.println("Error al recuperar la busqueda:" + cr.getResponse().getStatus());
success = false;
;
}
return success;
}
}
Spotify requiere que el token vaya como un header en cada request. Tienes que añadirlo con este formato (Típico de un token JWT):
"Authorization": "Bearer <token aquí>"
Answered by Pablo Lozano on February 17, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP