gohnjanotis
gohnjanotis

Reputation: 7565

AUTHENTICATION_FAILED when querying CloudKit public database with CloudKit Web Services API in Google Apps Script

I'm trying to use the CloudKit Web Services API to fetch Article records from my production CloudKit container's public database within Google Apps Script.

My request is based on the documentation on this page.

Here's my code:

// variables for the CloudKit request URL
var path = "https://api.apple-cloudkit.com";
var version = "1";
var container = "iCloud.com.companyname.My-Container-Name";
var environment = "production";
var database = "public";

var token = "8888888888888_my_actual_token_88888888888888888"

function showArticles() {

  // assemble the URL
  var url = path + "/database/" + version + "/" + container + "/" + environment + "/" + database + "/records/query?ckAPIToken=" + token;

  // specify the record type to query
  var query = {
    recordType: "Article"
  };

  // specify the payload for the POST request
  var payload = {
    query : query
  };

  // set up the fetch options for the fetch request
  var options = {
    method : "POST",
    payload : payload
   };

  // make the request
  var response = UrlFetchApp.fetch(url, options);

  Logger.log(response);
}

UrlFetchApp.fetch(url, options) fails with this error:

Request failed for https://api.apple-cloudkit.com/database/1/iCloud.<?>-Container-Name/development/public/records/query?ckAPIToken=8888888888888_my_actual_token_88888888888888888 returned code 401. Truncated server response: {"uuid":"7d8a8547-ad08-4090-b4b3-917868a42f6f","serverErrorCode":"AUTHENTICATION_FAILED","reason":"no auth method found"} (use muteHttpExceptions option to examine full response) (line 30, file "Code")

I've been troubleshooting for a few hours and I can't figure out what I'm doing wrong. I've tried it with a separate token on my development environment, too, and the same thing happens.

This page mentions the ckWebAuthToken parameter and says "if omitted and required, the request fails," but I can't find anything that says what requests require a ckWebAuthToken. I'm assuming I don't need ckWebAuthToken since the records I'm trying to access are in my container's public database, and I'm getting an AUTHENTICATION_FAILED error rather an AUTHENTICATION_REQUIRED error.

One part that confuses me is this URL that comes up in the error message:

https://api.apple-cloudkit.com/database/1/iCloud.<?>-Container-Name/development/public/records/query?ckAPIToken=8888888888888_my_actual_token_88888888888888888

I would expect it to be:

https://api.apple-cloudkit.com/database/1/iCloud.com.companyname.My-Container-Name/development/public/records/query?ckAPIToken=8888888888888_my_actual_token_88888888888888888

But I can't tell if that's actually the URL that's being requested, and when I log the url variable everything looks fine.

Thanks in advance for any troubleshooting tips or solutions!

UPDATE

I tried using Postman, and the request worked with same endpoint and POST data. It looks like the container component of the URL is getting corrupted by the Google Apps Script UrlFetchApp.fetch() method. The <?> seems to only show up when com. is in the URL.

Upvotes: 0

Views: 997

Answers (1)

gohnjanotis
gohnjanotis

Reputation: 7565

I'm not sure why this is the answer, but I was able to get it working by using JSON.stringify() on the payload in options:

var options = {
    method : "POST",
    payload : JSON.stringify(payload)
};

Upvotes: 0

Related Questions