Search code examples
pythondatabasedockerneo4jpy2neo

Neo4J with Py2neo: Unauthorized error HTTPS


I have Neo4J running on a Docker container in which I have mapped the internal container ports 7473 and 7687 to their respective host ports 7473 and 7687, 7474 is exposed but not mapped.

The Neo4J server configuration regarding network.

# Bolt connector dbms.connector.bolt.enabled=true
#dbms.connector.bolt.tls_level=OPTIONAL 
dbms.connector.bolt.listen_address=0.0.0.0:7687

# HTTP Connector. There must be exactly one HTTP connector. 
dbms.connector.http.enabled=true 
dbms.connector.http.listen_address=0.0.0.0:7474

# HTTPS Connector. There can be zero or one HTTPS connectors. 
dbms.connector.https.enabled=true 
dbms.connector.https.listen_address=0.0.0.0:7473

I was able to login to Neo4J's webclient through the browser and change the default password.

Regarding the Python code here's the line where I create the client.

self.client = py2neo.Graph(host    =ip_address,
                           username=username,
                           password=password,
                           secure  =use_secure,
                           bolt    =use_bolt)

As soon as I execute a query like this one.

node = Node("FooBar", foo="bar")
self.client.create(node)

I get the following Unauthorized exception.

py2neo.database.status.Unauthorized: https://localhost:7473/db/data/

Any idea on why this may be happening?


Solution

  • The solution was to call a separate authentication method provided by the library like this:

    auth_port = str(self._PORT_HTTPS if use_secure else self._PORT_HTTP)
    py2neo.authenticate(":".join([ip_address, auth_port]), username, password)
    

    It took me a while to get to this because at first, I thought the authentication was done automatically in the constructor and then I wasn't able to make the authentication method run because I was using the bolt port.