4

I have a problem in using of cURL as HTTP client with Neo4j. When I write this command : curl http://localhost:7474/db/data/ (or any URL, like http://localhost:7474/db/data/node/) Then I get this result in JSON format:

{
  "errors": [
    {
      "message": "No authorization token supplied.",
      "code": "Neo.ClientError.Security.AuthorizationFailed"
    }
  ],
  "authentication": "http://localhost:7474/authentication"
}
Kaan
  • 5,434
  • 3
  • 19
  • 41
ista9im
  • 79
  • 2
  • 10

4 Answers4

7

It seems that you have the authentication plugin enabled on your server. https://github.com/neo4j-contrib/authentication-extension

You should try something of the form

curl --user username:password http://localhost:7474/db/data/
Daniel Luca CleanUnicorn
  • 1,087
  • 1
  • 12
  • 30
4

Disagreeing with @hydraruiz, I guess you're running a Neo4j 2.2.0-M0x version. This one has authentication enabled by default. You first need to acquire a token by providing your username and password.

curl -H "Content-Type: application/json" -d '{"username":"neo4j", "password":"mypassword"}' http://localhost:7474/authentication
{
  "username" : "neo4j",
  "password_change" : "http://localhost:7474/user/neo4j/password",
  "password_change_required" : false,
  "authorization_token" : "53eaa48a972439012868a8d5463e0c3d",
  "authorization_token_change" : "http://localhost:7474/user/neo4j/authorization_token"
}

Subsequent calls to the REST API use the token in the Authorization header. According to the docs the value of the http Authorization header is Basic realm="Neo4j" plus the base64 encoded token prefixed by a colon. We can use command line tools for this: echo -n ":tokenstring" | base64. For simplicity I emit a trivial cypher statement match (n) return count(n):

curl -H "Authorization: Basic realm=\"Neo4j\" `echo -n ":53eaa48a972439012868a8d5463e0c3d" | base64`" \
    -H "Content-Type: application/json" \
    -d '{"statements":[{"statement":"match (n) return count(n)"}]}' \
    http://localhost:7474/db/data/transaction/commit

returns:

{"results":[{"columns":["count(n)"],"data":[{"row":[0]}]}],"errors":[]}

That means the authentication worked.

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
2

You can also disable authorization. In conf/neo4j-server.properties file:

# Require (or disable the requirement of) auth to access Neo4j
dbms.security.auth_enabled=false
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
0

As of Neo4j Community 4.4.7 (probably with earlier versions, too), here are two ways you can authenticate against Neo4j using curl. The first way (using --user) is easy to find examples of, but the second way (using "Authorization" header) wasn't clear how to construct

For both of these examples, assume the following:

  • username = "neo4j"
  • password = "am4zeftw"
  • IP address of Neo4j is 11.22.33.44
  • port is 7474

Also, the examples don't specify an actual query, they're both using "..." as dummy text since it isn't relevant to the examples.

1. --user parameter

Specify username and password as command-line input using --user flag, which would look like this: --user neo4j:am4zeftw and the entire command looks like this:

curl --user neo4j:am4zeftw \
-X POST -H 'Content-type: application/json' \
http://11.22.33.44:7474/db/data/transaction/commit -d "..."

2. "Authorization" header

This has a few steps:

  1. In order to authenticate this way, you first need to construct a base64 encoding of the username and password combination. If you structure a string in the correct format – "user:password" – and then run that into base64 you'll get a correctly formatted string. The example below generates bmVvNGo6YW00emVmdHc=:

    echo -n 'neo4j:am4zeftw' | base64
    bmVvNGo6YW00emVmdHc=
    
  2. Instead of using this:

    • --user neo4j:am4zeftw

    use this:

    • -H 'Authorization:Basic bmVvNGo6YW00emVmdHc=' (where bmVvNGo6YW00emVmdHc= came from the base64 step above)

    As an alternative, you can specify --header instead of -H.

    Here's an example:

    curl -H 'Authorization:Basic bmVvNGo6YW00emVmdHc=' \
    -X POST -H 'Content-type: application/json' \
    http://11.22.33.44:7474/db/data/transaction/commit -d "..."
    
Kaan
  • 5,434
  • 3
  • 19
  • 41