【问题标题】:Authorization error on call to server-to-server CloudKit Web Services调用服务器到服务器 CloudKit Web 服务时出现授权错误
【发布时间】:2016-12-20 23:11:02
【问题描述】:

我正在尝试实现与 CloudKit Web 服务的一些基本集成,following Apple's guide 用于验证请求。我从this 和this 得到了一些关于如何正确授权请求的帮助,并且似乎正确地遵循了所有步骤,但我仍然收到来自Apple 的401 AUTHENTICATION_FAILED 错误。

The endpoint I'm targeting 是一个 POST 端点,用于检索记录,给定记录名称。

我在代码中添加了 cmets 以显示我在各个阶段获得的输出,并且我使用了替代证书,因此我没有提供真正的私钥:

def self.signature(parameters, date, image_id)
    #date: 2016-08-14T14:32:20Z

    #parameters: {"records":[{"recordName":"7DBC4FAD-D18C-476A-89FB-14A515098F34"}]}

    encoded_parameters = Digest::SHA256.base64digest(parameters)

    #encoded_parameters: 6gmJ4AvmJgkNY4SJm6ImOxZaZ07J7cih/tRXI0zkRjQ=

    url_subpath = CloudKit.url_subpath

    #url_subpath: /database/1/iCloud.ProjectDent.TwIM/development/public/records/lookup

    message = date + ':' + encoded_parameters + ':' + url_subpath

    #message: 2016-08-14T14:23:35Z:6gmJ4AvmJgkNY4SJm6ImOxZaZ07J7cih/tRXI0zkRjQ=:/database/1/iCloud.ProjectDent.TwIM/development/public/records/lookup

    private_key = OpenSSL::PKey.read(File.read('altkey.pem'))

    signature = private_key.dsa_sign_asn1(OpenSSL::Digest::SHA256.digest(message))

    #signature: -? WX?xfc???ɔ???,?Ț?Փv?3+Xt!?$R?_Y?×*?,?3??Z-\#?ŭ?Ƿh

    encoded_signature = Base64.strict_encode64(signature)

    #encoded_signature: MEUCIFdYlHhmrxoIY8KW1tT6yZT17bYsP8ia09WTdpEzK1h0AiEA0yRSh39fWYHDlyqJLNgzhr9aLVwj2cWtkse3aA0tGZI=

    return encoded_signature
end

def self.headers(parameters, image_id)
    date = Time.now.utc.iso8601

    signature = self.signature(parameters, date, image_id)

    headers = {
        'X-Apple-CloudKit-Request-KeyID' => CloudKit.key_id,
        'X-Apple-CloudKit-Request-ISO8601Date' => date,
        'X-Apple-CloudKit-Request-SignatureV1' => signature
    }

    #headers (key id masked): {"X-Apple-CloudKit-Request-KeyID"=>"123456", "X-Apple-CloudKit-Request-ISO8601Date"=>"2016-08-14T14:32:20Z", "X-Apple-CloudKit-Request-SignatureV1"=>"MEUCIFdYlHhmrxoIY8KW1tT6yZT17bYsP8ia09WTdpEzK1h0AiEA0yRSh39fWYHDlyqJLNgzhr9aLVwj2cWtkse3aA0tGZI="}

    return headers
end

def self.fetch_image(image_id)
    url = CloudKit.url
    parameters = CloudKit.parameters(image_id).to_json
    headers = CloudKit.headers(parameters, image_id)

    begin
        response = RestClient.post(url, parameters, headers)
        puts 'response'
        puts response.code
        puts response.to_str
    rescue => e
        puts 'rescued: ' + e.to_s
    end
end

最终结果是:

获救:401 未授权

我不确定我在这里做错了什么 - 我似乎确实正确地遵循了所有步骤。希望这些日志可以帮助人们了解这可能出了什么问题。

【问题讨论】:

    标签: ruby-on-rails ruby openssl cloudkit cloudkit-web-services


    【解决方案1】:

    您需要在签名之前将参数转换为 JSON。并确保发送与签名副本相同的 JSON 字符串。

    【讨论】:

    • 谢谢。我已经对其进行了编辑以匹配此解决方案,该解决方案有效。希望有人能够将其用于自己的请求。
    猜你喜欢
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 2012-06-29
    • 2020-12-18
    相关资源
    最近更新 更多