OCP\Share "shareItem" method results in 500 method not found

Hello,

I am trying to implement a functionality in my NextCloud app which takes the path of a file and generates a public share link.
I am aware there is a button for this in the NextCloud GUI, but I’m developing this as a means of automating another task which needs the share URL to access and modify the file.

I am running NextCloud server from stable branch 19.

My ./appinfo/routes.php contains the following:

return [
    'routes' => [
        ['name' => 'share#generatePublicLink', 'url' => '/generatePublicLink', 'verb' => 'POST', 'controller' => [ShareController::class, 'generatePublicLink']],
    ]
];

I created a controller under ./lib/Controller/ShareController.php that contains the following:

<?php

// lib/Controller/ShareController.php

namespace OCA\DiscretePdfSigner\Controller;

use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\Share;
use OCP\Share\IShare;

class ShareController extends Controller {

    /**
     * Create a public read/write link for a given file.
     *
     * @NoAdminRequired
     *
     * @param string $path        Path to the file/folder which should be shared
     * @param string $shareWith   User/group id or email address with which the file should be shared
     * @param string $publicUpload Allow public upload to a public shared folder (true/false)
     * @param string $password    Password to protect public link share with (null in this case)
     *
     * @return DataResponse
     */
    public function generatePublicLink() {
        $requestData = json_decode(file_get_contents('php://input'), true);
        error_log('Received JSON: ' . print_r($requestData, true));

        // Validate input
        if (empty($requestData['path'])) {
            return new DataResponse(['error' => 'Invalid id'], 400);
        }

        // Set share type to public link (3)
        $shareType = Share::SHARE_TYPE_LINK;
        // Set permissions for public link (read/write)
        $permissions = 3;
        $password = null;


        $share = Share::shareItem(
            'file',
            $requestData['path'],
            $shareType,
            null,
            $permissions
        );
        $publicLink = $share->getLink();

        // Return the share Link as a response
        return new DataResponse(['publicLink' => $publicLink], 100);
    }    
}

I call this API through a JavaScript file under ./js/script.js :

function createSharedFileUrl(filePath) {
  const appName = "discretepdfsigner";

  // API endpoint URL
  const apiUrl = OC.generateUrl(
    `/apps/${appName}/generatePublicLink`
  );

  // Fetch options
  const fetchOptions = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "OCS-APIREQUEST": "true",
      Requesttoken: OC.requestToken,
    },
	body: JSON.stringify({ path: filePath }),
  };

  fetch(apiUrl, fetchOptions)
    .then((response) => response.json())
    .then((data) => {
      console.log("Public link:", data.publicLink);
      return data.publicLink;
    })
    .catch((error) => {
      console.error("Error:", error);
    });
}

When calling this method through the NextCloud GUI, the server console returns the following:

{"Exception":"Exception","Message":"Call to undefined method OCP\\Share::shareItem()","Code":0,"Trace":[{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/private\/AppFramework\/App.php","line":137,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/private\/AppFramework\/Routing\/RouteActionHandler.php","line":47,"function":"main","class":"OC\\AppFramework\\App","type":"::"},{"function":"__invoke","class":"OC\\AppFramework\\Routing\\RouteActionHandler","type":"->"},{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/private\/Route\/Router.php","line":297,"function":"call_user_func"},{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/base.php","line":1010,"function":"match","class":"OC\\Route\\Router","type":"->"},{"file":"\/home\/zehnder\/nextcloud19\/server\/index.php","line":37,"function":"handleRequest","class":"OC","type":"::"}],"File":"\/home\/zehnder\/nextcloud19\/server\/lib\/private\/AppFramework\/Http\/Dispatcher.php","Line":110,"Previous":{"Exception":"Error","Message":"Call to undefined method OCP\\Share::shareItem()","Code":0,"Trace":[{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/private\/AppFramework\/Http\/Dispatcher.php","line":170,"function":"generatePublicLink","class":"OCA\\DiscretePdfSigner\\Controller\\ShareController","type":"->"},{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/private\/AppFramework\/Http\/Dispatcher.php","line":100,"function":"executeController","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/private\/AppFramework\/App.php","line":137,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->"},{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/private\/AppFramework\/Routing\/RouteActionHandler.php","line":47,"function":"main","class":"OC\\AppFramework\\App","type":"::"},{"function":"__invoke","class":"OC\\AppFramework\\Routing\\RouteActionHandler","type":"->"},{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/private\/Route\/Router.php","line":297,"function":"call_user_func"},{"file":"\/home\/zehnder\/nextcloud19\/server\/lib\/base.php","line":1010,"function":"match","class":"OC\\Route\\Router","type":"->"},{"file":"\/home\/zehnder\/nextcloud19\/server\/index.php","line":37,"function":"handleRequest","class":"OC","type":"::"}],"File":"\/home\/zehnder\/nextcloud19\/server\/apps\/discretepdfsigner\/lib\/Controller\/ShareController.php","Line":46},"CustomMessage":"--"}

Even though I can see the method in the source code under ./server/lib/private/Share.php in my NextCloud installation:

public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions) {
		$backend = self::getB [...]

What am I doing wrong? I’m trying to parse through the documentation but I’ve had no luck so far.

Thanks in advance.