Skip to main content
 首页 » 编程设计

python之py2neo连接错误(认证错误)

2025年05月04日16haluo1

我一直在尝试连接到 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/ 

请您参考如下方法:

尝试:

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

如 py2neo 文档中所述:http://py2neo.org/v3/database.html#py2neo.database.Graph

authenticate 函数用于 HTTP 基本认证,例如当 neo4j 在网络服务器后面运行时。