[Node JS上的CloudKit服务器到服务器身份验证时出现401错误

问题描述 投票:0回答:1

我正在尝试使用服务器到服务器身份验证来查询我的公共CloudKit数据库。我已经根据Apple的文档生成了密钥,但是无论我做什么我都会收到此错误:

401 - Unauthorized

data: {
  uuid: '...',
  serverErrorCode: 'AUTHENTICATION_FAILED',
  reason: 'no auth method found'
}

据我所知,我已经将所有内容设置为per the docs,但显然我在做错什么。到目前为止,这是我的Node应用程序中提供的内容:


let date = moment().format('YYYY-MM-DD[T]HH:mm:ss[Z]')
let domain = 'https://api.apple-cloudkit.com'
let subpath = '/database/1/iCloud.<my container>/development/public/users/current'

let key = fs.readFileSync(__dirname +'/../eckey.pem', 'utf8')
let keyID = 'abc123...'

let requestBody = ''
let bodyHash = crypto.createHash('SHA256').update(requestBody).digest('base64')

let message = date+':'+bodyHash+':'+subpath

let signature = crypto.createSign('RSA-SHA256').update(message).sign(key, 'base64')

let headers = {
  'X-Apple-CloudKit-Request-KeyID': keyID,
  'X-Apple-CloudKit-Request-ISO8601Date': date,
  'X-Apple-CloudKit-Request-SignatureV1': signature
}

try{
  await axios.post(domain+subpath, requestBody, { headers: headers })
  console.log('--- :) ---')
}catch(error){
  console.log('=== :( ===')
  console.log(error)
}

我已经查看过this helpful SO post,但仍然遇到问题。

谁能看到我可能在做什么错吗?

cloudkit cloudkit-web-services server-to-server cloudkit-js
1个回答
0
投票

我不得不进行大量的故障排除才能弄清这一点,但是为了后代,这是我的错:

===修正#1 ===

我的日期生成的本地时间不准确,因为该格式表示Zulu / UTC时间(由于[C​​0])。

解决方法是将Z添加到当前时刻:

.utc()

===修正#2 ===

显然,Axios不喜欢我格式化请求的方式。将其更改为此(let date = moment().utc().format('YYYY-MM-DD[T]HH:mm:ss[Z]') baseURL分开)有效:

url

现在看来,使用这些修复程序现在可以很好地工作。

© www.soinside.com 2019 - 2024. All rights reserved.