2

I'd like to share a CloudKit asset in the public database with people who aren't using my app. From the CKAsset class, there's only a fileURL property, which points to a local file, so no URL given.

On the documentation for CloudKit Web Services, however, there is a "referencing existing assets" request, which in its' response gives a downloadURL to the asset file.

Calling this from my app gives me a 421 status code, which is described in documentation as "AUTHENTICATION_REQUIRED", as it requires a ckSession parameter to be given, which is a "session identifier of an authenticated user". It also passes a redirectURL, where a user can type in their iCloud login, and then be re-directed back to the web application, now with a session identifier.

Opening this URL inside the app didn't immediately return the session identifier, but instead expected the user to go through the login process in a browser, before receiving their session ID.

This seems totally off-track, and the wrong thing to be doing. I'm certainly not going to make my users log in to iCloud through a web interface. The only thing missing for that CloudKit Web Services url is the session ID. Is there a way for me to get that using the CloudKit framework? Or is there another way for me to find out this file's URL?

Andrew
  • 7,693
  • 11
  • 43
  • 81

1 Answers1

0

If you are using Apple's official CloudKit.js, here is the code to configure your CloudKit access:

window.addEventListener('cloudkitloaded', function() {
  console.log('Cloudkit loaded');
  CloudKit.configure({
    locale: 'en-us',
    containers: [{
      containerIdentifier: 'iCloud.com.getYoursInXcode',
      apiTokenAuth: {
        apiToken: 'getThisInCloudkitDashboardApiAccess',
        persist: true //Set cookie
      },
      environment: 'production'
    }]
  });
  // Do your thing here

  })

If you are fetching records in your backend, it is still better to get your API token instead of using the Server-To-Server process. For example, query records in GO, to fetch a record, compose your request body like this

sampleRequest = []byte('{
        "resultsLimit":"4",
        "desiredKeys": ["recordTitle","recordDescription"],
        "query": {
        "recordType": "myRecord",
        "sortBy": {
        "fieldName": "rank",
        "ascending": true
    }
    }
        }')

And send it to the following Url

http.NewRequest("POST", ""https://api.apple-cloudkit.com/database/1/iCloud.com.yourIdentifier/production/public/records/query?ckAPIToken=YOURTOKEN", bytes.NewBuffer(RequestBody))
JSNoob
  • 1,477
  • 2
  • 18
  • 31