401-Unauthorized developer token in Apple Music API

I am trying to access API of Apple Music API

> https://developer.apple.com/documentation/applemusicapi


After creating the JWT developer token, when i try to access the API, An error of 401 - Unauthorized occurs.


I am doing it in the following way :


1) Registering a new music identifier > https://help.apple.com/developer-account/#/devce5522674?sub=dev0416b9004


2) Creating a MusicKit identifier and private key > https://help.apple.com/developer-account/#/devce5522674 also downloading the private key file.


3) Getting the Kid (Key Id) and issuer (Team Id) .


4) Running the following code to generate the token :


const jwt     = require("jsonwebtoken");          
const privateKey = fs.readFileSync("AuthKey_KEY_ID.p8").toString();      

const teamId     = TEAM_ID_HERE;      
const keyId      = KEY_ID_HERE;    

 const jwtToken = jwt.sign({}, privateKey, {                       
                                             algorithm: "ES256",                    
                                             expiresIn: "120d",                    
                                             issuer: teamId,
                                             header: {                             
                                                       alg: "ES256",
                                                      kid: keyId                       
                                             }             
                              });     
console.log(jwtToken);



And after that checking the code using the curl command :


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


I am not sure what i am missing here. is it something related to access or is it my code.


P.S : I am the Account Holder, so it is not an issue related to role.



Thanks

Post not yet marked as solved Up vote post of akh123 Down vote post of akh123
13k views

Replies

Exact same issue here.

I tried different way to generate the developer token (like https://github.com/mkoehnke/musickit-token-encoder or programmatically with https://github.com/IBM-Swift/Swift-JWT) but still the same 401 results for all search API calls (even with the official Adding Content to Apple Music sample code from Apple).

Note that generating a UserToken is also failing with my developerToken.

I've done the exact same thing and couldn't get it to work. I noticed it adds in a `typ: "JWT"` to the header, so maybe it's not supposed to have that.

I am having the same problem. Tired creating the token using 4 different approaches.


When I check the token using


https://jwt.io/


I noticed that I get an error that the signature is invalid. It's the same result despite which method I use to create the token.


I know this isn't a solution, but hoped that someone might be able to help point me in the right direction.

Hey Folks, I am facing the very same issue. Did you manage to find a way forward?

I'm also having the exact same issue. Would love to hear about any updates/resolutions if folks have found any.

I have exactly the same problem:

< HTTP/2 401

Been trying for three days now.

I know I did everything correct, still fails.

My "Apple Music" subscription is the free 3 month trial but I guess that should not matter?

It seems we are many developers facing the same problem, it must be some problem at Apple's end?


You can check your signed token here:

https://shawntabrizi.com/JWT-Decoder/

When I check my token(s), I get exactly the correct data back, team ID, key ID etc.

I have the same issue. Has anyone been able to resolve this?

I had the exact same issue, however, it's working for me today. I don't know if that was an issue with Apple or what. I'm using the same code as the original poster.

Still getting a 401, and am also using the same code

Having the same issue. Tried creating a new Key ID and regenerating the jwtToken but still not working. Every request returns a 401 Unauthorized

Same here! Would love to see a solution to this, looks like there is plenty of people with this issue...

OK, this embarrasing, but I had an extra character at the end of my Team ID. After correcting it, I am now able to successfully call the API. My apologies!

Hi all,


I have the same issue. I hope that you succeeded since the post exist.

So how did you do ?
I don't understand why it doesn't work.

Hi All,


I also have the same issue.

How did you solve it?
Can someone help me

Per the documentation on the library, please put the payload as the first argument in the sign method


https://github.com/auth0/node-jsonwebtoken


Payload should include

{
"iss": "DEF123GHIJ",
"iat": 1437179036,
"exp": 1493298100
}

per https://developer.apple.com/documentation/applemusicapi/getting_keys_and_creating_tokens