我正在使用Apple的CloudKit Catalog example,而我只是想使身份验证有效。我想在浏览器窗口(而不是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
,但遇到相同的错误。
有人知道我在做什么错吗?
似乎世界上只有大约5个人了解CloudKit JS API,并且他们全部都被重新分配以使用ARKit,而他们却忽略了Stack Overflow。 ;)
为了后代,我知道我有两件事是错误的。
== 1 ==
您必须忽略弃用警告并使用版本1的JS文件(ck/1/cloudkit.js
):
https://cdn.apple-cloudkit.com/ck/1/cloudkit.js
== 2 ==
尽管文档说了什么,实际上您不能在容器配置中指定身份验证按钮<div id="">
。当我更改HTML以使用默认的Apple<div>
ID时,Sign In按钮开始为我显示:
<div id="apple-sign-in-button"></div> <div id="apple-sign-out-button"></div>
我希望可以帮助别人。 :)