CloudKit JS的“Hello World”示例:找不到身份验证方法

3

我正在学习苹果公司的CloudKit Catalog实例,并且我只是想要让认证过程正常工作。我希望在浏览器窗口(而不是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,但我得到了相同的错误。

有人知道我做错了什么吗?

2个回答

4

似乎全世界只有5个人了解CloudKit JS API,而所有了解该API的人都已被重新分配到ARKit上工作,并忽略了Stack Overflow。;)

为了纪念这一时刻,我学到了两件事情做错了。

== 1 ==

您需要忽略弃用警告并使用版本1 JS文件(ck/1/cloudkit.js):

https://cdn.apple-cloudkit.com/ck/1/cloudkit.js

== 2 ==

尽管文档中指出可以在容器配置中指定身份验证按钮的<div id="">,但实际上并不行。当我改用默认的苹果<div> ID时,登录按钮开始显示。

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

我希望这能帮助到其他人。 :)

0

在 cloudkit.js 中似乎存在一些问题(当不使用 Node 时?)它尝试保存身份验证 cookie。您可以通过类似以下方式来解决这个问题:

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。


谢谢,Greg。我现在已经对这些有了很多了解。我发现使用CloudKit JS存在很多风险,而直接使用CloudKit Web Services API与我选择的JS框架相结合会更好得多。自从我做出这个改变后,情况好多了。 - Clifton Labrum

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接