I was able to figure out a workaround solution that solved my question. First, I had to allow "Anonymous" users the permission to POST to my custom REST resource. This allowed my resource to be accessed by the POST request programmatically-called by my Guzzle client.
In my REST resource, I am checking for the user's permission, which restrict access for users without the permissions defined (in my case, "access content").
/**
* Provides a resource to get bundles by entity.
*
* @RestResource(
* id = "distribution_content_ingestion",
* label = @Translation("Distribution Content Ingestion"),
* serialization_class = "Drupal\my_module\Normalizer\JsonDenormalizer",
* uri_paths = {
* "canonical" = "/entity/distribution_ingest/{distribution_id}",
* "https://www.drupal.org/link-relations/create" = "/entity/distribution_ingest/{distribution_id}"
* }
* )
*/
class DistributionIngestResource extends ResourceBase {
/**
* A current user instance.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* Constructs a Drupal\rest\Plugin\ResourceBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param array $serializer_formats
* The available serialization formats.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* A current user instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
array $serializer_formats,
LoggerInterface $logger,
AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->getParameter('serializer.formats'),
$container->get('logger.factory')->get('rest'),
$container->get('current_user')
);
}
/**
* Responds to POST requests.
*
* Returns a list of bundles for specified entity.
*
* @param string $distribution_id
* The Distribution ID to ingest.
* @param array $data
* The data from the POST.
*
* @return \Drupal\rest\ResourceResponse
* Throws exception expected.
*/
public function post($distribution_id, $data) {
// You must to implement the logic of your REST Resource here.
// Use current user after pass authentication to validate access.
if (!$this->currentUser->hasPermission('access content')) {
throw new AccessDeniedHttpException();
}
/**
* Some code here.
*/
}
}