2017-06-13 27 views
3

Im試圖創建一個開發人員令牌,該令牌是用於Apple音樂身份驗證的ES256 JWT。 (Here如何創建用於Apple Music的JWT

即時通訊使用紅寶石和JWT的寶石,但在創建令牌後,我用樣品私鑰來模擬只是爲了演示429錯誤與蘋果音樂

require 'jwt' 

    payload = {:iss => 'CapExdTeam', :iat => '1497335982', :exp => '1513112982'} 

    priv = "-----BEGIN PRIVATE KEY----- 
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgU208KCg/doqiSzsVF5sknVtYSgt8/3oiYGbvryIRrzSgCgYIKoZIzj0DAQehRANCAAQfrvDWizEnWAzB2Hx2r/NyvIBO6KGBDL7wkZoKnz4Sm4+1P1dhD9fVEhbsdoq9RKEf8dvzTOZMaC/iLqZFKSN6 
-----END PRIVATE KEY-----" 
    ecdsa_key = OpenSSL::PKey::EC.new(priv) 

    token = JWT.encode payload, ecdsa_key, 'ES256', { :kid => "CapExedKid", :alg => "ES256" } 
    puts token 

    `curl -v -H 'Authorization: Bearer #{token}' "https://api.music.apple.com/v1/catalog/us/songs/203709340" 

林進行身份驗證時得到一個401錯誤

+0

有同樣的問題 – CarpenterBlood

+0

我有幾乎相同的代碼。可能是爲exp字段設置錯誤時間。 –

回答

4

我使用這個腳本,它完美的作品 https://github.com/pelauimagineering/apple-music-token-generator

+1

嘿,謝謝你,我一直在爲這個解決方案工作幾天,並且正在消耗我們的開發人員技術代碼級別支持之一,並問他們如何去做。我沒有太多的快速經驗,所以我失去了所有這些其他語言。再次感謝!!! – CarpenterBlood

+0

真是太高興了:您對如何做到這一點的描述很簡單,直接而且有效!看過各種各樣的選擇後,看到很多東西感到非常高興。非常感謝你。 – mmm

+1

原來的問題(和我自己找到它)正在尋找一個Ruby解決方案。提供的鏈接在Python中,並且效果很好...只需要一個Ruby解決方案。 –

0

這裏是一個工作的Ruby實現。用您的keyId和teamId進行調用,訪問您的私鑰文件並繼續。

class AppleMusic 

    @auth_token 
    @validity_start 
    @validity_end 


    def initialize(keyId, teamId, options ={}) 
     appleKeyId = keyId 
     appleTeamId = teamId 
     @validity_start = Time.now.to_i 
     @validity_end = Time.now.to_i + 43200 # 12 hours in seconds... 

     # Build up the headers 
     header = { 
      'typ' => 'JWT',   # MUST BE SPECIFIED... Apple doesn't tell you this! 
      'alg' => 'ES256', 
      'kid' => appleKeyId 
     } 

     # Build up the payload 
     body = { 
      'iss' => appleTeamId, 
      'iat' => @validity_start, 
      'exp' => @validity_end 
     } 

     # This should be installed manually on the server somewhere 
     # TODO: Add some protection around the file's existance, set the name & location 
     # as some type of configuration key. 
     file = File.read('lib/assets/AuthKey_xxxxxxx.p8') 
     key = OpenSSL::PKey::EC.new(file) 
     key.check_key 

     @auth_token = JWT.encode(body, key, 'ES256', header) 
     @auth_token 
    end 

    def auth_token 
     @auth_token 
    end 

    def auth_header 
     "Bearer #{@auth_token}" 
    end 

    def validity_start 
     @validity_start 
    end 

    def validity_end 
     @validity_end 
    end 
end 
1

基於@ DanDevine的答案,這裏是一個更紅寶石/ OO方法:

require "openssl" 

# Example: 
# 
# token = AppleMusic::Token.new(key_id: "...", team_id: "...", keyfile: "lib/assets/AuthKey_xxxxxxx.p8") 
# token.auth_token 
# token.auth_header 
# 
module AppleMusic 
    class Token 
    attr_reader :key_id, :team_id, :keyfile 

    # Keyfile should be an IO type that responds to `read` 
    def initialize(key_id:, team_id:, keyfile:) 
     @key_id = key_id 
     @team_id = team_id 
     @keyfile = keyfile 
    end 

    def auth_token 
     @auth_token ||= fetch_auth_token 
    end 

    def auth_header 
     "Bearer #{auth_token}" 
    end 

    protected 

    def fetch_auth_token 
     header = { 
     typ: "JWT", # Must be specified; not in documentation 
     alg: "ES256", 
     kid: key_id 
     } 

     body = { 
     iss: team_id, 
     iat: Time.now.to_i, 
     exp: Time.now.to_i + 43_200 # 12hrs 
     } 

     JWT.encode(body, auth_key, 'ES256', header) 
    end 

    def auth_key 
     key = OpenSSL::PKey::EC.new(keyfile.read) 
     key.check_key 
     key 
    end 
    end 
end 
相關問題