NOTE It's necessary to prepare an TEAM_ID, MUSIC_ID, and a secret file in advance. Please confirm Apple Developer Website.
It can be set by either an ENV variable or an config/initializers/apple_music.rb:
AppleMusic.configuredo |config|
config.secret_key_path='./AuthKey_MUSIC_ID.p8'# or ENV['APPLE_MUSIC_SECRET_KEY_PATH']config.team_id='YOUR TEAM_ID'# or ENV['APPLE_MUSIC_TEAM_ID']config.music_id='YOUR MUSIC_ID'# or ENV['APPLE_MUSIC_MUSIC_ID']config.storefront='jp'# or ENV['APPLE_MUSIC_STOREFRONT'] ('us' by default)end
Problem:
I was seeing that after having my server running a while, search results for songs that had been returning results were suddenly an empty array. Inspecting the API's results before the gem returned them showed a 401 error.
Looking at the gem's connection.rb, the client is being memoized with the config.authentication_token which expires after a day.
One suggestion is to improve the handling of API responses to alert when the request is no longer authenticated. This would have helped me see the real problem immediately.
Another suggestion is to check if the token is expired and initialize a new client with a fresh token.
TypeError: no implicit conversion of nil into String
from apple_music (c78cf4ffa6fe) lib/apple_music/song/attributes.rb:30:in `parse'
from apple_music (c78cf4ffa6fe) lib/apple_music/song/attributes.rb:30:in `initialize'
from apple_music (c78cf4ffa6fe) lib/apple_music/resource.rb:37:in `new'
from apple_music (c78cf4ffa6fe) lib/apple_music/resource.rb:37:in `initialize'
from apple_music (c78cf4ffa6fe) lib/apple_music/resource.rb:27:in `new'
from apple_music (c78cf4ffa6fe) lib/apple_music/resource.rb:27:in `build'
from apple_music (c78cf4ffa6fe) lib/apple_music/relationship.rb:20:in `block in build_list'
from apple_music (c78cf4ffa6fe) lib/apple_music/relationship.rb:20:in `map'
from apple_music (c78cf4ffa6fe) lib/apple_music/relationship.rb:20:in `build_list'
from apple_music (c78cf4ffa6fe) lib/apple_music/relationship.rb:11:in `initialize'
from apple_music (c78cf4ffa6fe) lib/apple_music/search_result.rb:16:in `new'
from apple_music (c78cf4ffa6fe) lib/apple_music/search_result.rb:16:in `initialize'
from apple_music (c78cf4ffa6fe) lib/apple_music/search.rb:20:in `new'
from apple_music (c78cf4ffa6fe) lib/apple_music/search.rb:20:in `search'
from apple_music (c78cf4ffa6fe) lib/apple_music.rb:35:in `search'
from apple_music (c78cf4ffa6fe) lib/apple_music/song.rb:74:in `search'
ArgumentError: wrong number of arguments (given 3, expected 2)
from /usr/local/bundle/gems/apple_music-0.3.1/lib/apple_music/album.rb:47:in `get_relationship'
Same happens when calling ::Apple::Artist.related_albums(artist.id) for instance
PS: If you need that feature now you can call AppleMusic::Album.get_relationship(raw.id, :artists) to get the related artists of an album for instance