【问题标题】:401 error when try to connect to Lighthouse API尝试连接到 Lighthouse API 时出现 401 错误
【发布时间】:2014-11-24 19:03:35
【问题描述】:

我正在尝试使用 C# 代码连接到 Lighthouse api。这是 php 示例https://support.lighthouserocks.com/hc/en-gb/articles/201319732-API-The-Basics,它描述了如何执行此操作。但我失败了。我尝试了 NetworkCredentials & send in the Header 但仍然有 401 Unauthorized access to it,这里是代码:

public string RequestResponse()
{
    HttpWebRequest webRequest = WebRequest.Create(HomeUrl) as HttpWebRequest;
    webRequest.Method = "GET";
    webRequest.ContentType = "application/json";
    webRequest.ServicePoint.Expect100Continue = false;
    webRequest.Timeout = 20000;

    string auth = CreateAuthorization("domain.lhlive.com", "user", "token");
    webRequest.Headers["auth"] = "Basic " + auth;

    //webRequest.Credentials = new NetworkCredential("user", "token");
    //webRequest.PreAuthenticate = true;
    //webRequest.Headers.Add("auth", "user, token");
    webRequest.Accept = "application/vnd.lighthouse.v1.hal+json";

    Stream responseStream = null;
    StreamReader responseReader = null;
    string responseData = "";
    try
    {
        WebResponse webResponse = webRequest.GetResponse();
        responseStream = webResponse.GetResponseStream();
        responseReader = new StreamReader(responseStream);
        responseData = responseReader.ReadToEnd();
    }
    finally
    {
        if (responseStream != null)
        {
            responseStream.Close();
            responseReader.Close();
        }
    }

    return responseData;
}

public void Test()
{
    using (var client = new WebClient())
    {
        client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0) (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
        client.Headers["ContentType"] = "application/json";
        client.Headers["Accept"] = "application/vnd.lighthouse.v1.hal+json";
        //client.Headers["Lighthouse Username"] = "user";
        //client.Headers["API Key"] = "token";
        client.Headers["WWW-Authenticate"] = "user, token";

        byte[] arr = client.DownloadData("https://domain.lhlive.com/contacts");

        Console.WriteLine("--- WebClient result ---");
        Console.WriteLine(arr.Length);
    }
}

有人知道我该怎么做吗?

【问题讨论】:

    标签: c# api lighthouse


    【解决方案1】:

    很难说,因为我无法访问 Lighthouse,但请尝试以下操作(注意如何设置 auth 标头)。

    var webRequest = WebRequest.Create("http://some.endpoint.com/") as HttpWebRequest;
    webRequest.Method = "GET";
    webRequest.Accept = "application/vnd.lighthouse.v1.hal+json";
    webRequest.ContentType = "application/json";
    webRequest.Headers["Authorization"] = string.Format("Basic {0}",
        Convert.ToBase64String(Encoding.Default.GetBytes(
            string.Format("{0}:{1}", "your username", "your API key"))));
    
    var response = webRequest.GetResponse();
    var stream = response.GetResponseStream();
    var data = (new StreamReader(stream)).ReadToEnd();
    

    在这种情况下,您的 Authorization 标头看起来像 "Basic eW91ciB1c2VybmFtZTp5b3VyIEFQSSBrZXk="

    【讨论】:

    • 上线 var response = webRequest.GetResponse();我有以下错误附加信息:远程服务器返回错误:(405)方法不允许。用你的代码
    • 嗯,资源不允许动词。更改网址或方法?至少它不再是未经授权的。
    • 我换成另一种方法domain.lhlive.com/contacts/1 又是返回401错误...
    • 只是忘记在您的示例中添加接受方法,之后它就可以正常工作了
    猜你喜欢
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    相关资源
    最近更新 更多