7

I am having trouble getting access to the users Apple Music. The error I am getting is

 [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""
2019-02-04 19:14:37.250467+0900 SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"
2019-02-04 19:14:37.252008+0900 [core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""
2019-02-04 19:14:37.252051+0900  SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"
2019-02-04 19:14:37.253604+0900  SSAccountStore: Unable to get the local account. error = Error Domain=SSErrorDomain Code=100 "Cannot connect to iTunes Store" UserInfo={NSLocalizedDescription=Cannot connect to iTunes Store}

However the weird part of this code is that I am also able to retrieve the Music User Token. Is there sth that I am missing? Any help is appreciated.

    static func auth(){
        let cloudServiceController = SKCloudServiceController()

        let developerToken = "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyz"


        SKCloudServiceController.requestAuthorization { status in
            guard status == .authorized else { return }
        }

        cloudServiceController.requestCapabilities { capabilities, error in
            guard capabilities.contains(.musicCatalogPlayback) else { return }
        }

        cloudServiceController.requestUserToken(forDeveloperToken: developerToken, completionHandler: { token, error in

            guard let token = token else { return }
            UserDefaults.standard.set(token, forKey: "MUSIC_USER_TOKEN")
            UserDefaults.standard.set(developerToken, forKey: "DEVELOPER_TOKEN")
            print("Music User Token:", token)
        })
    }
Tech IRIS
  • 113
  • 9
  • 3
    Did you resolve your issue? I'm struggling with the same error right now.. – dschu Jun 21 '19 at 09:57
  • I get the same errors. Do you happen to have different AppleIds for the iCloud and iTunesStore? I do and wonder if it may be because of that... – Mike Pollard Oct 10 '19 at 21:10

2 Answers2

0

I think you have to call cloudServiceController.requestUserToken once user has authorised after completion handler for SKCloudServiceController.requestAuthorization

infiniteLoop
  • 2,135
  • 1
  • 25
  • 29
  • OP would be getting a different error if `SKCloudServiceController.requestAuthorization.requestAuthorization` is failing. That error is this: `Error Domain=SKErrorDomain Code=6 "The requesting app does not have the necessary permissions" UserInfo={NSLocalizedDescription=The requesting app does not have the necessary permissions}`. So this isn't the issue. (Although I agree OP's given code isn't organizing the callbacks' executions correctly) – Xavier L. Dec 23 '19 at 23:13
0

I was having this same issue until I removed Bearer from the beginning of developerToken.

OP's code example has developerToken set to "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxyz", so I can only assume if OP is including Bearer at the beginning or not.

So to be more clear, this is what I was doing before:

asyncAskMyServerToGenerateMyAppleMusicDeveloperJWTDevToken { rawDevToken in
    let formattedDeveloperToken = "Bearer \(rawDevToken)"
    SKCloudServiceController().requestUserToken(forDeveloperToken: formattedDeveloperToken)
    { possibleToken, _ in
        if let userMusicToken = possibleToken
        {
            YayIGotIt.forTheWin(userMusicToken)
        }
    }
}

And this is what I did to make it actually work:

asyncAskMyServerToGenerateMyAppleMusicDeveloperJWTDevToken { rawDevToken in

    //Not prepending "Bearer " anymore
    SKCloudServiceController().requestUserToken(forDeveloperToken: rawDevToken)
    { possibleToken, _ in
        if let userMusicToken = possibleToken
        {
            YayIGotIt.forTheWin(userMusicToken) //This actually fires now
        }
    }
}
Xavier L.
  • 709
  • 5
  • 21