Error 405: Unable to Upload a Custom Playlist Cover Image

落爺英雄遲暮 提交于 2021-01-29 18:34:38

问题


I want to upload a playlist image from my iOS application to the Spotify Web API.

I followed the instructions on the docs page: I requested the ugc-image-upload, playlist-modify-public and playlist-modify-private scopes, added the content type header and made a jpeg Base64 String. But I keep getting back a 405 Method Not Allowed error. I am able to create a image with the same URL and Authorization Token on android. I already tried to add the android generated Base64 String to my swift request, but this did not work either.

This is a swift code sample generated by Postman where I tried to debug this request. The Base64 String is a 100x100 orange jpeg image which was generated in my android app and worked there. The decoding process does work on this site and the result is the expected image: https://www.base64decode.org In Postman I cannot get this to work, too.

let headers = [
    "Authorization": "Bearer […]",
    "Content-Type": "image/jpeg",
    "Cache-Control": "no-cache"
]

let postData = NSData(data: "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCABkAGQDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAj/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFgEBAQEAAAAAAAAAAAAAAAAAAAkK/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8AvABJ9oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAf/9k=".data(using: String.Encoding.utf8)!)

let request = NSMutableURLRequest(url: NSURL(string: "https://api.spotify.com/v1/users/[…]/playlists/[…]/images")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
    if (error != nil) {
        print(error)
    } else {
        let httpResponse = response as? HTTPURLResponse
        print(httpResponse)
    }
})

dataTask.resume()

And here is the httpResponse:

Optional(<NSHTTPURLResponse: 0x282fdf300> { URL: https://api.spotify.com/v1/users/[…]/playlists/[…]/images } { Status Code: 405, Headers {
    "Access-Control-Allow-Origin" =     (
        "*"
    );
    "Cache-Control" =     (
        "private, max-age=0"
    );
    "Content-Length" =     (
        0
    );
    Date =     (
        "Fri, 20 Jul 2018 14:12:01 GMT"
    );
    Via =     (
        "1.1 google"
    );
    "access-control-allow-credentials" =     (
        true
    );
    "access-control-allow-headers" =     (
        "Accept, Authorization, Origin, Content-Type, Retry-After"
    );
    "access-control-allow-methods" =     (
        "GET, POST, OPTIONS, PUT, DELETE, PATCH"
    );
    "access-control-max-age" =     (
        604800
    );
    allow =     (
        "GET, HEAD, OPTIONS, PUT"
    );
    "alt-svc" =     (
        clear
    );
} })

Since the Base64 String, Authorization Token and URL are the same I assume there is something wrong with my request body or header fields.

Thanks in advance!


回答1:


Found the problem: It should perform a PUT request instead of a POST request. After changing this one line it did work.

request.httpMethod = "PUT"


来源:https://stackoverflow.com/questions/51445491/error-405-unable-to-upload-a-custom-playlist-cover-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!