错误 405:无法上传自定义播放列表封面图片
Error 405: Unable to Upload a Custom Playlist Cover Image
我想将播放列表图像从我的 iOS 应用程序上传到 Spotify Web API。
我按照 docs page 上的说明进行操作:我请求了 ugc-image-upload
、playlist-modify-public
和 playlist-modify-private
范围,添加了内容类型 header 并制作了一个 jpeg Base64 字符串。但我不断收到 405 Method Not Allowed
错误。我能够在 android 上创建具有相同 URL 和授权令牌的图像。我已经尝试将 android 生成的 Base64 字符串添加到我的 swift 请求中,但这也不起作用。
这是 Postman 生成的 swift 代码示例,我在其中尝试调试此请求。 Base64 字符串是一个 100x100 橙色 jpeg 图像,它是在我的 android 应用程序中生成并在那里工作的。解码过程在这个网站上确实有效,结果是预期的图像:https://www.base64decode.org 在 Postman 中我也无法让它工作。
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()
这里是 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
);
} })
由于 Base64 字符串、授权令牌和 URL 相同,我假设我的请求 body 或 header 字段有问题。
提前致谢!
发现问题:它应该执行 PUT 请求而不是 POST 请求。
更改这一行后它确实起作用了。
request.httpMethod = "PUT"
我想将播放列表图像从我的 iOS 应用程序上传到 Spotify Web API。
我按照 docs page 上的说明进行操作:我请求了 ugc-image-upload
、playlist-modify-public
和 playlist-modify-private
范围,添加了内容类型 header 并制作了一个 jpeg Base64 字符串。但我不断收到 405 Method Not Allowed
错误。我能够在 android 上创建具有相同 URL 和授权令牌的图像。我已经尝试将 android 生成的 Base64 字符串添加到我的 swift 请求中,但这也不起作用。
这是 Postman 生成的 swift 代码示例,我在其中尝试调试此请求。 Base64 字符串是一个 100x100 橙色 jpeg 图像,它是在我的 android 应用程序中生成并在那里工作的。解码过程在这个网站上确实有效,结果是预期的图像:https://www.base64decode.org 在 Postman 中我也无法让它工作。
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()
这里是 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
);
} })
由于 Base64 字符串、授权令牌和 URL 相同,我假设我的请求 body 或 header 字段有问题。
提前致谢!
发现问题:它应该执行 PUT 请求而不是 POST 请求。 更改这一行后它确实起作用了。
request.httpMethod = "PUT"