Cannot read a file attached in another App; 403 Error

Hello everyone,

I want to read and modify a .docx file that is in a Attachment field in another App.

I know it is saved as a file.json, and I got fileKey of this file.

But I cannot access the file.json of another App as it results in a 403 error.

To solve this, I gave full permission like this:

App Permissions:

Failed attempt to get the file.json file:

Resulting 403 Forbidden Error
403_Error

This is my code:
:zap: Note url variable is hardcoded with {subdomain} & AppID

(function () {
  'use strict';
  var mergeDocumentEvents = ['app.record.edit.submit', 'app.record.create.submit'];

  kintone.events.on(mergeDocumentEvents, function (event) {
    var lookupID = kintone.app.getLookupTargetAppId('Doc_lookup');
    var recordID = event.record['Doc_lookup'].value;
    // console.log("Template App ID:",text,"Lookup code",event.record['Doc_lookup']);
    var docBody = {
      'app': lookupID,
      'id': recordID
    }
    console.log("currentLookupId", lookupID, "currentRecordId", recordID);
    kintone.api(kintone.api.url('/k/v1/record', true), 'GET', docBody, function (resp) {
      var fileKey = resp.record.Attachment.value[0].fileKey;
      var url = 'https://{subdomain}.kintone.com/k/12/file.json?fileKey=' + fileKey;
      console.log("fileurl", url);
      var xhr = new XMLHttpRequest();
      xhr.open('GET', url);
      xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
      xhr.responseType = 'blob';
      xhr.onload = function () {
        if (xhr.status === 200) {
          // success
          var blob = new Blob([xhr.response]);
          var windowUrl = window.URL || window.webkitURL;
          var blobUrl = windowUrl.createObjectURL(blob);
          console.log("bloburl-----", blobUrl);
        } else {
          // error
          console.log(xhr.responseText);
        }
      };
      xhr.send();
    }, function (error) {
      // error
      console.log(error);
    });
  });
})();

Please help me.

Hello @unudeveloper ,

This is most likely because the URL specified for download files is incorrect.

The URL needs to be:

https://{subdomain}.kintone.com/k/v1/file.json

  • https://{subdomain}.kintone.com/k/v1/file.json

instead of

https://{subdomain}.kintone.com/k/12/file.json

So it needs to be:

var url = 'https://{subdomain}.kintone.com/k/v1/file.json?fileKey=' + fileKey;

Download File API - Kintone Developer Program

Could you try this and see if it works?

1 Like

Thank you, I will try it.

Thanks Chris.
It works well.
Best Regards.