0

I am trying to put comment to a node using POST method to the endpoint api/v1.1/comments

Here is the API file:

<?php

/**
 * @file
 * Contains \Drupal\restful_example\Plugin\resource\comment\Comments__1_0.
 */

namespace Drupal\lsd_restful_api\Plugin\resource\comment;

use Drupal\restful\Plugin\resource\ResourceEntity;
use Drupal\restful\Plugin\resource\ResourceInterface;
use Drupal\lsd_restful_api\Plugin\resource\comment\DataProviderCommentNew;

/**
 * Class Comments__1_1
 * @package Drupal\restful_example\Plugin\resource\comment
 *
 * @Resource(
 *   name = "comments:1.1",
 *   resource = "comments",
 *   label = "Comments",
 *   description = "Export the comments with all authentication providers.",
 *   authenticationTypes = {
 *      "token",
 *     "basic_auth"
 *   },
 *   authenticationOptional = TRUE,
 *   dataProvider = {
 *     "entityType": "comment",
 *     "bundles": FALSE,
 *   },
 *   majorVersion = 1,
 *   minorVersion = 1
 * )
 */
class Comments__1_1 extends ResourceEntity implements ResourceInterface {

  /**
   * {@inheritdoc}
   */
  protected function publicFields() {
    $public_fields = parent::publicFields();

    $public_fields['nid'] = array(
      'property' => 'node',
      'sub_property' => 'nid',
    );

    // Add a custom field for test only.

      $public_fields['comment_text'] = array(
        'property' => 'body',
        'sub_property' => 'value',
      );


    return $public_fields;
  }

  /**
   * {@inheritdoc}
   */
  protected function dataProviderClassName() {
    return DataProviderCommentNew::class;
  }

}

And this is the request body I am sending with POST request with access-token and authentication in Headers:

{
  "nid" : "38565",
  "body" :  "this is comment from API"
}

And here is the error I am getting :

{
    "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4",
    "title": "You do not have access to create a new resource.",
    "status": 403,
    "detail": "Forbidden"
}

As you can see above error clearly shows that I do not have access to create new resource. So I have crossed check the permissions and everything seems fine with the commenting permission.
Not sure what is happening or what is wrong with the API. Please help me on this. It would really be appreciated. Thank you in advance.

Kind regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.