title: Apple Auth not working on Ruby on Rails Backend
body: <p>I need to authentificate user with Apple Auth in mobile application. I got <strong>IdentityToken</strong>, <strong>User</strong> and <strong>AuthCode</strong> from api request from mobile app.</p>
  
  <p>I need to authentificate user and get its email. My class <strong>AppleId</strong> always returns: "<strong>invalid_client</strong>".</p>
  
  <p>Maybe i have some mistakes in my logic?</p>
  
  <pre><code>
  class AppleId
  
      URL = 'https://appleid.apple.com/auth/token'
      ISSUER = 'https://appleid.apple.com'
      TEAM_ID = '6Z******38'
      KEY_ID = '33******KP'
      IDENTIFIER = 'ru.k*************on'
      PRIVATE_KEY = &lt;&lt;-PEM
  -----BEGIN PRIVATE KEY-----
  MIGTA**********z7
  -----END PRIVATE KEY-----
  PEM
  
      def initialize
        @private_key = OpenSSL::PKey::EC.new PRIVATE_KEY
      end    
  
      def authenticate(code)
        make_request(code)
      end
  
      private 
  
      def make_request(code) 
        uri = URI.parse(URL) 
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
  
        request = Net::HTTP::Post.new(uri.request_uri)
  
        params =  {
          client_id: IDENTIFIER,
          client_secret: client_secret_jwt,
          code: code,
          grant_type: 'authorization_code' 
         }
  
         request.set_form_data(params)
         response = http.request(request)
         response.body
      end
  
      def client_secret_jwt
        jwt = JSON::JWT.new(
          iss: TEAM_ID,
          aud: ISSUER,
          sub: IDENTIFIER,
          iat: Time.now,
          exp: 1.minutes.from_now
        )
        jwt.kid = KEY_ID
        JWT.encode jwt, @private_key, 'ES256'
      end
  
  end
  </code></pre>
  
  <p>In my Controller:</p>
  
  <pre><code>authCode = params[:auth_code]
  
  AppleId.new.authenticate(authCode)
  =&gt; "{\"error\":\"invalid_client\"}"
  
  </code></pre>