简体   繁体   English

py2neo连接错误(身份验证错误)

[英]py2neo connection error (authentication error)

I've been trying to connect to neo4j, authenticate and create nodes with various properties. 我一直试图连接到neo4j,进行身份验证并创建具有各种属性的节点。 neoj is working fine in http://localhost:7474/ . neoj在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) 我在下面用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")      

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/

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 如py2neo文档中所述: http ://py2neo.org/v3/database.html#py2neo.database.Graph

The authenticate function is used for HTTP basic auth, eg when neo4j runs behind a webserver. authenticate功能用于HTTP基本身份验证,例如,当neo4j在Web服务器后面运行时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM