我一直在尝试连接到neo4j,验证并创建具有各种属性的节点。 neoj在http://localhost:7474/中工作得很好。我已经在下面填写了mypassword以获取我的实际密码(我非常确定它不是密码问题,因为我已通过cmd提示连接到neo4j)
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")
我得到的错误如下,有人可以帮忙吗?
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/
答案 0 :(得分:2)
尝试:
graph = Graph("http://localhost:7474/db/data/", user="neo4j", password="mypassword")
如py2neo文档中所述:http://py2neo.org/v3/database.html#py2neo.database.Graph
authenticate
函数用于HTTP基本身份验证,例如当neo4j在网络服务器后面运行时。