我正在研究 Apple 的CloudKit 目录示例,我只是想让身份验证工作。我想在浏览器窗口(不是 Node JS)中运行这个 JS 代码。我从他们的网站上获取了他们的代码,如下所示:
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<script>
window.addEventListener('cloudkitloaded', function() {
CloudKit.configure({
containers: [{
containerIdentifier: 'iCloud.com.example.CloudKitCatalog',
apiToken: 'b86f0b5db29f04f45badba0366f39b7130a505f07765b5ba3a2ceb0cb3d96c0c',
persist: true,
environment: 'production',
signInButton: { id: 'sign-in-button', theme: 'black' },
signOutButton: { id: 'sign-out-button', theme: 'black' }
}]
})
var container = CloudKit.getDefaultContainer()
//-----
function gotoAuthenticatedState(userIdentity) {
var name = userIdentity.nameComponents
if(name) {
displayUserName(name.givenName + ' ' + name.familyName)
} else {
displayUserName('User record name: ' + userIdentity.userRecordName)
}
container
.whenUserSignsOut()
.then(gotoUnauthenticatedState)
}
//-----
function gotoUnauthenticatedState(error) {
if(error && error.ckErrorCode === 'AUTH_PERSIST_ERROR') {
showDialogForPersistError()
}
displayUserName('Unauthenticated User')
container
.whenUserSignsIn()
.then(gotoAuthenticatedState)
.catch(gotoUnauthenticatedState)
}
// Check a user is signed in and render the appropriate button.
return container.setUpAuth()
.then(function(userIdentity) {
// Either a sign-in or a sign-out button was added to the DOM.
// userIdentity is the signed-in user or null.
if(userIdentity) {
gotoAuthenticatedState(userIdentity)
} else {
gotoUnauthenticatedState()
}
})
})
</script>
<script src="https://cdn.apple-cloudkit.com/ck/2/cloudkit.js" async></script>
<div id="sign-in-button"></div>
<div id="sign-out-button"></div>
</body>
</html>
但我不断收到这两个错误:
cloudkit.js:14 GET https://api.apple-cloudkit.com/database/1/iCloud.com.example.CloudKitCatalog/production/public/users/caller?ckjsBuildVersion=2005ProjectDev34&ckjsVersion=2.6.1&clientId=735f8b19-3218-4493-80e4-7ab0b39041ac 401 (Unauthorized)
(anonymous) @ cloudkit.js:14
紧接着...
cloudkit.js:14 Uncaught (in promise) t {
_ckErrorCode: "AUTHENTICATION_FAILED",
_uuid: "3b5cf33d-d56d-414f-83a4-6f320cd915b2",
_reason: "no auth method found",
_serverErrorCode: "AUTHENTICATION_FAILED",
_extensionErrorCode: undefined, …
}
我觉得这应该是容易的部分,但我什setUpAuth()
至无法让第一个功能工作。我也尝试过我自己的 CloudKit containerIdentifier
,apiToken
但我得到了同样的错误。
有谁知道我做错了什么?