首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Neo4jClient事务-事务完成时的8月错误

Neo4jClient事务-事务完成时的8月错误
EN

Stack Overflow用户
提问于 2015-07-20 14:52:11
回答 1查看 326关注 0票数 0

我正在尝试使用Neo4jClient事务支持的最新预发行版(Neo4jClient 1.1.0-Tx00008)。我尝试过最简单的示例,但每次事务完成时,似乎总是会出现身份验证错误(Neo4j Community2.2.2或2.3)。以下代码:

代码语言:javascript
复制
        var graphClient = new GraphClient(new Uri("http://user:password@localhost:7474/db/data"));
        graphClient.Connect();

        using (var transaction = graphClient.BeginTransaction())
        {
            graphClient.Cypher.Create("(n:TestNode {Name:'TestNode2'})").ExecuteWithoutResults(); 
            graphClient.Cypher.Create("(n:TestNode {Name:'TestNode3'})").ExecuteWithoutResults(); 
            transaction.Commit();     
        }

当事务提交被调用时,或者如果我排除了transaction.Commit()并允许范围结束,总是会导致此错误:

代码语言:javascript
复制
Received an unexpected HTTP status when executing the request.

The response status was: 401 Unauthorized

The response from Neo4j (which might include useful detail!) was: <html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx</center>
</body>
</html>

在不使用任何事务范围的情况下,节点可以很好地创建。我已经查看了源代码中的单元测试,但是在如何调用它方面没有任何区别。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-20 17:13:19

您现在必须使用AuthWrapper,我担心它看起来会像:

代码语言:javascript
复制
private class HttpClientAuthWrapper : IHttpClient
{
    private readonly AuthenticationHeaderValue _authenticationHeader;
    private readonly HttpClient _client;

    public HttpClientAuthWrapper(string username, string password)
    {
        _client = new HttpClient();
        if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            return;

        var encoded = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password));
        _authenticationHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(encoded));
    }

    public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
    {
        if(_authenticationHeader != null)
            request.Headers.Authorization = _authenticationHeader;
        return _client.SendAsync(request);
    }
}

然后使用如下方式:

代码语言:javascript
复制
var client = new GraphClient(
    new Uri("http://localhost.:7474/db/data"), 
    new HttpClientAuthWrapper("user", "pass#")
    );
client.Connect();

这将使所有事务使用正确的身份验证头。谢谢你把它提高到吉顿,以及-我们可以调查,使它得到妥善解决!

(由by -代码全部来自:http://geekswithblogs.net/cskardon/archive/2015/03/27/upgrading-your-neo4j-from-xxx-to-2.2.0ndashhaving-authorization-trouble.aspx)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31519633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档