2

我正在研究 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 containerIdentifierapiToken但我得到了同样的错误。

有谁知道我做错了什么?

4

2 回答 2

2

似乎世界上只有大约 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 ID时,登录按钮开始出现:<div>

<div id="apple-sign-in-button"></div>
<div id="apple-sign-out-button"></div>

我希望这对其他人有所帮助。:)

于 2020-04-10T18:55:38.853 回答
0

当 cloudkit.js 尝试保存身份验证 cookie 时(不使用 Node 时?)似乎存在一些问题。你可以用这样的方式伪造你的方式:

function MyAuth()
{
}

MyAuth.prototype.putToken = function (containerIdentifier, authToken)
{
  console.log('save cookie: ' + containerIdentifier + ' = ' + authToken);
  sessionStorage.setItem('auth.' + containerIdentifier, JSON.stringify(authToken));
}

MyAuth.prototype.getToken = function(containerIdentifier)
{
  console.log('load cookie: ' + containerIdentifier);
  return JSON.parse(sessionStorage.getItem('auth.' + containerIdentifier));
}

var gAuth = new MyAuth();

并设置您的容器:

CloudKit.configure({
  containers: [<container config>],
  services: {authTokenStore: gAuth}
});

sessionStorage 将保存 cookie 直到浏览器关闭。使用 json 转换是因为当您退出时,它会将值设置为 null,该值会保存为“null”,然后加载不正确。您还可以将其本地存储在哈希或实际 cookie 中。

此外,您的配置不正确,因为您已展平并删除了 apiTokenAuth 成员:

CloudKit.configure({
  containers: [{
    containerIdentifier: 'iCloud.com.example.CloudKitCatalog',
    environment: 'production',
    apiTokenAuth: {
      apiToken: 'b86f0b5db29f04f45badba0366f39b7130a505f07765b5ba3a2ceb0cb3d96c0c',
      persist: true,
      signInButton: { id: 'sign-in-button', theme: 'black' },
      signOutButton: { id: 'sign-out-button', theme: 'black' }
    }
  }]
})

这适用于 cloudkit.js 的 v1 或 v2。

于 2020-11-30T15:36:54.937 回答