Type in your question below and we'll check to see what answers we can find...
Loading article...
Submitting...
If you couldn't find any answers in the previous step then we need to post your question in the community and wait for someone to respond. You'll be notified when that happens.
Simply add some detail to your question and refine the title if needed, choose the relevant category, then post.
Before we can post your question we need you to quickly make an account (or sign in if you already have one).
Don't worry - it's quick and painless! Just click below, and once you're logged in we'll bring you right back here and post your question. We'll remember what you've already typed in so you won't have to do it again.
Plan
Premium
Country
Australia
Device
Laptop
Operating System
API
My Question or Issue
When sending a response through the try me link for Upload a Custom Playlist Cover Image, using a correct playlist ID and OAuth Token I received a 400 error with no explanation. Is this URL link working at the moment?
I have also been attempting to do this through my own integration with the Spotify API, attempting various image formats but all of them are returning a 400 error. The only time I received a different error was when the image I was sending was over the 256k limit.
Here is the code that I am using
function main() {
// Playlist is a playlist object from spotify
// imageBase64 is the image encoded in base 64, obtained through Jimp's getBase64Async function
const response = setPlaylistCoverArt(playlist.href, imageBase64)
}
// Public
async function setPlaylistCoverArt(playlistHref, image) {
const url = playlistHref + "/images";
const response = await this.makeApiRequest(url, () => this.getJPEGHeaders(image, "PUT"))
return response;
}
async function makeApiRequest(url, getData) {
let response = await fetch(url, getData());
if (response.status == 401 || response.status == 400) {
await this.refreshAccessToken();
return await fetch(url, getData());
}
return response;
}
function getHeaders(body, method) {
const header = {
method: method,
headers: {
Authorization: "Bearer " + this.accessToken,
"Content-Type": "application/json",
},
};
if (method === "POST" || method === "DELETE" || method === "PUT") {
header.body = JSON.stringify(body);
}
return header;
}
function getJPEGHeaders(body, method) {
const headers = this.getHeaders(body, method);
headers.headers["Content-Type"] = "image/jpeg"
return headers;
}
async function refreshAccessToken() {
// Gets new refresh token
}
Log the URL, headers and body of the request. Then share the logs with me. The body may begin with "data:image/jpeg;base64,". If so, remove it.
There are also other issues with your code. `getHeaders` converts the body to a JSON string, but you shouldn't convert the data to a JSON string for this endpoint.
In `makeApiRequest`, you refresh the access token if the response status code is 401 or 400. This is wasteful. Instead, keep track of whether the access token is expired using the expiration date and refresh the token if it is expired before making the request. Furthermore an expired access token is not the only cause of a 400 or 401 status code.
Thanks Peter. Your suggestion to remove data:image/jpeg;base64, from the front of time image data solved my problem! Also thanks for the suggestions, I will definitely add that to my code.
Hey there you, Yeah, you! 😁 Welcome - we're glad you joined the Spotify Community! While you here, let's have a fun game…