1 回答

TA貢獻1875條經驗 獲得超5個贊
創建服務器到服務器密鑰是重要的第一步,但為了在此之后發出 HTTP 請求,您必須對每個請求進行簽名。
這有點令人費解,但您必須仔細構建簽名標頭以包含在您發出的每個請求中。我不熟悉如何在 Python 中執行此操作,但這是我在 NodeJS 中執行此操作的方法,這可能會有所幫助:
//Get the timestamp in a very specific format
let date = moment().utc().format('YYYY-MM-DD[T]HH:mm:ss[Z]')
//Construct the subpath
let endpoint = '/records/lookup'
let path = '/database/1/iCloud.*****/development/public'
let subpath = path+endpoint
//Get the key file
let privateKeyFile = fs.readFileSync('../../'+SECRET_FILE_KEY, 'utf8')
//Make a string out of your JSON query
let query = {
? recordType: '[my record type]'
}
let requestBody = JSON.stringify(query)
//Hash the query
let bodyHash = crypto.createHash('sha256').update(requestBody, 'utf8').digest('base64')
//Assemble the components you just generated in a special format
//[Current date]:[Request body]:[Web service URL subpath]
let message = date+':'+bodyHash+':'+subpath
??
//Sign it
let signature = crypto.createSign('RSA-SHA256').update(message).sign(privateKeyFile, 'base64')
//Assemble your headers and include them in your HTTP request
let headers = {
? 'X-Apple-CloudKit-Request-KeyID': KEY_ID,
? 'X-Apple-CloudKit-Request-ISO8601Date': date,
? 'X-Apple-CloudKit-Request-SignatureV1': signature
}
起初這有點毛茸茸,但我只是將所有這些東西放在一個函數中,每當我需要發出請求時我都會重用它。
Apple 的文檔幾乎已被廢棄,如今很難找到有關 CloudKit Web 服務的好幫助。
添加回答
舉報