Owen
Owen

Reputation: 169

py2neo connection error (authentication error)

I've been trying to connect to neo4j, authenticate and create nodes with various properties. neoj is working fine in http://localhost:7474/. I've subbed mypassword in for my actual password below (I'm pretty sure its not a password issue as I've connected to neo4j through cmd prompt)

import py2neo
from py2neo import authenticate,Graph, Node

def creatNodeWithLabelProperties():
    print("Start - Creating Node with Label and Properties")

    # Authenticate the user using py2neo.authentication

    py2neo.authenticate("localhost:7474", "neo4j", "mypassword")

    # Connect to Graph and get the instance of Graph

    graph = Graph("http://localhost:7474/db/data/")


    node1 = Node("FirstLabel", name="MyPythonNode1", neo4j_version="2.2")

    node2 = Node("FirstLabel", "SecondLabel",name="MyPythonNode2", neo4j_version="2.2")

    resultNodes = graph.create(node1, node2)

    for index in range(len(resultNodes)):
        print("Created Node - ", index, ", ", resultNodes[index])
    print("End - Creating Node with Label and Properties")


def createNodeWithLabelPropertiesWithCast():
    print("Start - Creating Node with Label and Properties")

    py2neo.authenticate("localhost:7474", "neo4j", "mypassword")

    graph = Graph("http://localhost:7474/db/data/")
    #Define a LIST of Labels
    labels = [ 'FirstLabel' ,'SecondLabel' ]
    #Define a DICTIONARY
    properties = {'name':'MyPythonNode2', 'neo4j_version':'2.2'}
    #CAST the node and invoke graph.create method.
    node = Node.cast(labels,properties)
    resultNode, = graph.create(node)
    print("Node - ", resultNode)

    print("End - Creating Node with Label and Properties")                                  

if __name__ == '__main__':
    print("Start Creating Nodes")
    creatNodeWithLabelProperties
    createNodeWithLabelPropertiesWithCast()
    print("End Creating Nodes")      

The error I get is as follow, can anyone kindly help?

Start Creating Nodes
Start - Creating Node with Label and Properties
Traceback (most recent call last):
  File "C:\Users\OdhranH7\AppData\Local\Programs\Python\Python36\lib\site-packages\py2neo\database\__init__.py", line 318, in __new__
    inst = cls.__instances[key]
KeyError: (<class 'py2neo.database.Graph'>, <ServerAddress settings={'host': 'localhost', 'http_port': 7474}>, 'data')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\OdhranH7\AppData\Local\Programs\Python\Python36\lib\site-packages\py2neo\database\http.py", line 154, in get
    response = self.__base.get(headers=headers, redirect_limit=redirect_limit, **kwargs)
  File "C:\Users\OdhranH7\AppData\Local\Programs\Python\Python36\lib\site-packages\py2neo\packages\httpstream\http.py", line 966, in get
    return self.__get_or_head("GET", if_modified_since, headers, redirect_limit, **kwargs)
  File "C:\Users\OdhranH7\AppData\Local\Programs\Python\Python36\lib\site-packages\py2neo\packages\httpstream\http.py", line 943, in __get_or_head
    return rq.submit(redirect_limit=redirect_limit, **kwargs)
  File "C:\Users\OdhranH7\AppData\Local\Programs\Python\Python36\lib\site-packages\py2neo\packages\httpstream\http.py", line 452, in submit
    return Response.wrap(http, uri, self, rs, **response_kwargs)
  File "C:\Users\OdhranH7\AppData\Local\Programs\Python\Python36\lib\site-packages\py2neo\packages\httpstream\http.py", line 489, in wrap
    raise inst
py2neo.packages.httpstream.http.ClientError: 401 Unauthorized

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "CreateNode.py", line 53, in <module>
    createNodeWithLabelPropertiesWithCast()
  File "CreateNode.py", line 38, in createNodeWithLabelPropertiesWithCast
    graph = Graph("http://localhost:7474/db/data/")
  File "C:\Users\OdhranH7\AppData\Local\Programs\Python\Python36\lib\site-packages\py2neo\database\__init__.py", line 327, in __new__
    use_bolt = version_tuple(inst.__remote__.get().content["neo4j_version"]) >= (3,)
  File "C:\Users\OdhranH7\AppData\Local\Programs\Python\Python36\lib\site-packages\py2neo\database\http.py", line 157, in get
    raise Unauthorized(self.uri.string)
py2neo.database.status.Unauthorized: http://localhost:7474/db/data/

Upvotes: 2

Views: 4134

Answers (1)

Martin Preusse
Martin Preusse

Reputation: 9369

Try:

graph = Graph("http://localhost:7474/db/data/", user="neo4j", password="mypassword")

As described in the py2neo docs: http://py2neo.org/v3/database.html#py2neo.database.Graph

The authenticate function is used for HTTP basic auth, e.g. when neo4j runs behind a webserver.

Upvotes: 7

Related Questions