1 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個(gè)贊
創(chuàng)建服務(wù)器到服務(wù)器密鑰是重要的第一步,但為了在此之后發(fā)出 HTTP 請(qǐng)求,您必須對(duì)每個(gè)請(qǐng)求進(jìn)行簽名。
這有點(diǎn)令人費(fèi)解,但您必須仔細(xì)構(gòu)建簽名標(biāo)頭以包含在您發(fā)出的每個(gè)請(qǐng)求中。我不熟悉如何在 Python 中執(zhí)行此操作,但這是我在 NodeJS 中執(zhí)行此操作的方法,這可能會(huì)有所幫助:
//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
}
起初這有點(diǎn)毛茸茸,但我只是將所有這些東西放在一個(gè)函數(shù)中,每當(dāng)我需要發(fā)出請(qǐng)求時(shí)我都會(huì)重用它。
Apple 的文檔幾乎已被廢棄,如今很難找到有關(guān) CloudKit Web 服務(wù)的好幫助。
添加回答
舉報(bào)