8

I am trying to let people like tracks, albums or playlists on Apple Music from a webpage.

I understand the manual on this page: https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/AppleMusicWebServicesReference/SetUpWebServices.html

Until I hit the part where I should use the requestUserToken(forDeveloperToken:completionHandler:) method from SKCloudServiceController in the StoreKit framework, since there is no StoreKit framework available for outside iOS/macOS/tvOS/etc.

How is this token generated? Is there any other way I can generate this Music User Token manually?

I know it is a (very) longshot, but maybe someone figured it out!

nishanthshanmugham
  • 2,967
  • 1
  • 25
  • 29
Reinier
  • 262
  • 3
  • 13
  • Token can be generated from here: https://www.example-code.com/swift/jwt_ecc_create.asp even it has ES256 encryption but it accepts pem file and apple provide p8 format file. I have searched a lot but didn't get any solution for conversion from p8 file to pem. – TechValens May 23 '18 at 09:49

2 Answers2

5

The new Apple MusicKit JS library allows you to create Music User Token's from your Developer Token as seen here outside of iOS. Once you've loaded the Library you can use the authorize method to allow a user to authenticate and generate a token.

document.addEventListener('musickitloaded', function() {
  // MusicKit global is now defined
  MusicKit.configure({
    developerToken: 'DEVELOPER-TOKEN',
    app: {
      name: 'My Cool Web App',
      build: '1978.4.1'
    }
  });

  let music = MusicKit.getInstance();

  music.authorize().then(musicUserToken => {
    console.log(musicUserToken);
  });
});
Kole Myers
  • 61
  • 1
  • 6
  • Does this token need to be encrypted if I want to save it to my database for each user? – zerohedge Sep 26 '18 at 04:56
  • It returns 'undefined' to me – Pradeep Singh Oct 11 '18 at 20:22
  • I have tried all methods like `'music.musicUserToken'` but still doesn't work for me – Pradeep Singh Oct 13 '18 at 05:43
  • I made a basic example of getting the js sdk setup [here](https://github.com/KoleMyers/apple-musickit-example). Once configured the `music` variable should be available in the console and you can run `music.authorize().then(musicUserToken => { console.log(musicUserToken); });` to login. You will need an Apple Music account to login. Hopefully this helps – Kole Myers Oct 15 '18 at 20:54
  • @KoleMyers Hi, I am trying to authorize user but after login nothing happens. Empty window displayed. – Degard Jul 02 '19 at 17:58
  • @Degard - hmm It looks like it's still working for me. Are you trying the setup in that github project I linked? if so open an issue on the project with your details and I'll try to figure it out. – Kole Myers Jul 03 '19 at 19:24
1

I have tried the approach within my iOS app.

You can follow this link generate music token

This is basically python script. You can easily create a music developer token from your mac.

Abdul Yasin
  • 3,480
  • 1
  • 28
  • 42
  • 1
    Hey Thanks for adding your answer! I forgot all about this question, but luckily I did find that repo too eventually. – Reinier Oct 24 '17 at 12:05
  • @Reinier ,@AbdulYasin - this doesn't allow for the generation of user tokens outside of the US though , does it? – zerohedge Dec 27 '17 at 09:06
  • @AbdulYasin Thanks for answering. I'm referring to User tokens not developer tokens. Would it be possible for you to show how this can be done in the answer? I mean, where do you prompt the user to enter his credentials before acquiring their token? Note: I'm not talking about iOS. I'm talking about a web-app. – zerohedge Dec 27 '17 at 09:09
  • @zerohedge You will find the above link much helpful. To generate the user token (a MusicKit private key in a *.p8 file, a 10-digit key identifier in your Apple Developer account and your 10-digit Apple Developer Account Team ID), you can visit Apple member centre. If you have a member account, you can proceed with the generating the stuffs. – Abdul Yasin Dec 27 '17 at 10:19
  • @AbdulYasin I’ve already been there and it’s still unclear to me how to identify for example a Windows user who has Apple Music on his iPhone from a Python web app. Thanks for your help in any case – zerohedge Dec 27 '17 at 10:22
  • This answer is wrong, as it generates a developer token, not a user token. The dev token is just a stepping stone, as it identifies which client app is asking, the user token allows the client access to the service via a particular account. – w0mbat May 16 '23 at 18:14