1

I want to create a mobile application using my drupal application. For the communication I use the module services. The connection system works with success. This return this result.

    {
  "sessid": "98gnwpE1GeguxN1y4t0WkWhDzK-C-Ashbn5GExj9l7M",
  "session_name": "SESS7609adb25a00cf561a9d882a31e81d29",
  "token": "aK4L6D1APRH16lGSAirIaZVGR02T5gs8DeE5jffYCKI",
  "user": {
    "uid": "30",
    "name": "admin",
    "mail": "[email protected]",
    "theme": "",
    "signature": "",
    "signature_format": "filtered_html",
    "created": "1486745824",
    "access": "1487091377",
    "login": 1487154223,
    "status": "1",
    "timezone": "Africa/Ouagadougou",
    "language": "en",
    "picture": null,
    "init": "[email protected]",
    "data": {
      "ckeditor_default": "t",
      "ckeditor_show_toggle": "t",
      "ckeditor_width": "100%",
      "ckeditor_lang": "en",
      "ckeditor_auto_lang": "t",
      "contact": 1
    },
    "uuid": "14899bf3-1a0d-42b5-934e-650471d7da54",
    "roles": {
      "2": "authenticated user",
      "3": "administrator"
    },
    "rdf_mapping": {
      "rdftype": [
        "sioc:UserAccount"
      ],
      "name": {
        "predicates": [
          "foaf:name"
        ]
      },
      "homepage": {
        "predicates": [
          "foaf:page"
        ],
        "type": "rel"
      }
    }
  }
}

Now I want to create a node type article and I use this method

POST: http://localhost/drupal-7/?q=my_endpoint/node.json
{"type":"article","title":"BEVVtDcZmL","language":"und"}

But the result

[
  "Access denied for user anonymous"
]
. I know it normal because he don't know who wants to create the node. Now I question is how to parse sessionId or token to have the permission to create my node? Thanks

1 Answer 1

1
  1. First thing you need to do is login via Services (unless you allow anonymous users permission to create content), which is a POST call to ?q=[my-endpoint]/user/login.json
  2. Once logged in, you need to retrieve a new CSRF token (this new token should be returned within the login result)
  3. Then make a POST call to ?q=[my-endpoint]/node.json, sending along something like this:

-

{
  title: "Hello world",
  type: "article",
  uid: 123,
  body: {
    und: [
      { value: 'How are you?' }
    ]
  }
}

Attach an X-CSRF-Token header to the request using the new token value generated after logging in.

That should then return something like this to you:

{
  nid: 456,
  uri: /* ... */
}

I have a blog post with examples of most of the Services Resources here: http://tylerfrankenstein.com/code/drupal-services-examples

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.