尝试获取最近的曲目时,Apple Music API 收到 403 'Forbidden'

Apple Music API getting 403 'Forbidden' when trying to fetch recent tracks

嘿,大千世界的人们!

我正在尝试使用以下 HTTP 请求从 Apple Music API 检索所有最近的曲目: static let AMRecentTracks = "https://api.music.apple.com/v1/me/recent/played/tracks"

可耻的是,我一直收到错误消息 403 forbidden。这没有任何意义,因为我能够使用以下方法成功执行搜索请求: static let AMMusicURL = "https://api.music.apple.com/v1/catalog/"

如有任何帮助,我们将不胜感激。似乎关于这个具体案例的信息很少。我查过了。

我正在使用 Bearer 和令牌发出请求。 static let prefix = "Bearer " 我在上面附加了 developer token.

下面是我的代码:

func fetchRecentPlayedResources(_ complition: @escaping (Result<[Song], Error>)->Void) {
    let suffix = "?types=songs"
    
    guard let searchURL     = URL(string: Request.AMRecentTracks + suffix) else { return }
    var musicRequest        = URLRequest(url: searchURL)
    musicRequest.httpMethod = HTTPRequest.GET
    musicRequest.addValue(HTTPRequest.prefix + self.developerToken, forHTTPHeaderField: HTTPRequest.authorization)
    
    
    URLSession.shared.dataTask(with: musicRequest) { [weak self] (data, response, error) in
        guard let self = self else { return }
        if let error = error {
            complition(.failure(error))
        } else {
            if let data = data {
                self.parseSongs(with: data) { songs in
                    complition(.success(songs))
                }
            }
        }
    }.resume()
}

您需要在发送个性化请求时添加两个令牌,iTunes API 文档中的示例显示:

curl -v -H 'Music-User-Token: [music user token]' -H 'Authorization: Bearer [developer token]' "https://api.music.apple.com/v1/catalog/us/songs/203709340"

因此,也许仅添加开发人员令牌作为授权是不够的,这就是您收到 403 错误的原因。同样,正如文档所述 - 这是由于授权无效或不充分。