Hey Stack Overflow neighborhood,
I am presently dealing with a problem with the combination of the Apple Music API in my utility. I’ve adopted the documentation and pointers supplied by Apple, however I am encountering issues with authentication and parsing the Apple Music monitor particulars.
This is a abstract of the difficulty:
When making a request to fetch Apple Music monitor particulars utilizing the supplied API endpoint, I am receiving the next error: “Error parsing Apple Music monitor particulars: The info could not be learn as a result of it is not within the appropriate format.”
Moreover, the response standing code is 401, indicating an unauthorized request.
I’ve already tried the next troubleshooting steps:
Verified that the MusicKit App Service is enabled in my app’s App ID configuration.
Confirmed that the bundle identifier used within the app matches the one within the App ID configuration.
Ensured that I am signed in with a sound Apple ID within the simulator and system I am testing.
Regardless of these efforts, the difficulty persists. I imagine I’ve adopted the proper procedures for automated developer token era, however there is likely to be one thing lacking or misconfigured.
I am searching for help and steering from the Stack Overflow neighborhood on how you can resolve this subject. Any insights, strategies, or troubleshooting steps can be drastically appreciated.
Thanks prematurely in your assist!
func fetchAppleMusicTrackDetails(from mediaURL: URL, completion: @escaping (AppleMusicTrack?) -> Void) {
let parts = URLComponents(url: mediaURL, resolvingAgainstBaseURL: false)
guard let itemID = parts?.queryItems?.first(the place: { $0.title == "id" })?.worth else {
completion(nil)
return
}
fetchStorefront { storefront in
guard let storefront = storefront else {
print("storefront subject")
completion(nil)
return
}
let regex = attempt! NSRegularExpression(sample: "s*(.*?)s*", choices: .caseInsensitive)
let trimmedStorefront = regex.stringByReplacingMatches(in: storefront, choices: [], vary: NSMakeRange(0, storefront.depend), withTemplate: "")
let encodedStorefront = trimmedStorefront.replacingOccurrences(of: " ", with: "%20")
let lookupURLString = "https://api.music.apple.com/v1/catalog/(encodedStorefront)/songs/(itemID)"
print(lookupURLString)
guard let lookupURL = URL(string: lookupURLString) else {
print("lookupURL subject")
completion(nil)
return
}
URLSession.shared.dataTask(with: lookupURL) { (knowledge, response, error) in
if let httpResponse = response as? HTTPURLResponse {
print("Standing code: (httpResponse.statusCode)")
}
if let error = error {
print("Error fetching Apple Music monitor particulars: (error.localizedDescription)")
completion(nil)
return
}
guard let knowledge = knowledge else {
completion(nil)
print("no knowledge")
return
}
let responseString = String(knowledge: knowledge, encoding: .utf8)
print("Response knowledge: (responseString ?? "No knowledge")")
do {
let responseJSON = attempt JSONSerialization.jsonObject(with: knowledge, choices: []) as? [String: Any]
let outcomes = responseJSON?["results"] as? [[String: Any]]
let trackData = outcomes?.first
print(responseJSON)
// Extract the mandatory monitor data from the JSON response
guard let title = trackData?["trackName"] as? String,
let artist = trackData?["artistName"] as? String,
let album = trackData?["collectionName"] as? String,
let artworkURLString = trackData?["artworkUrl100"] as? String,
let artworkURL = URL(string: artworkURLString),
let trackURLString = trackData?["previewUrl"] as? String,
let trackURL = URL(string: trackURLString) else {
completion(nil)
return
}
// Obtain the paintings picture
URLSession.shared.dataTask(with: artworkURL) { (artworkData, _, _) in
guard let artworkData = artworkData, let artworkImage = UIImage(knowledge: artworkData) else {
completion(nil)
return
}
let monitor = AppleMusicTrack(title: title, artist: artist, album: album, paintings: artworkImage, trackURL: trackURL)
completion(monitor)
}.resume()
} catch {
print("Error parsing Apple Music monitor particulars: (error.localizedDescription)")
completion(nil)
}
}.resume()
}
}
func fetchStorefront(completion: @escaping (String?) -> Void) {
SKCloudServiceController().requestStorefrontCountryCode { storefrontCountryCode, error in
if let error = error {
print("Error fetching storefront nation code: (error.localizedDescription)")
completion(nil)
return
}
guard let countryCode = storefrontCountryCode else {
print("nation code error")
completion(nil)
return
}
completion(countryCode)
}
}