401 error when try to connect to Lighthouse API

774 views Asked by At

I'm trying to connect to the Lighthouse api using C# code. This is the php example https://support.lighthouserocks.com/hc/en-gb/articles/201319732-API-The-Basics which it describe how to do this. But I'm fails with it. I was try both NetworkCredentials & send in the Header but still have 401 Unauthorized access to it, here is the code:

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);
    }
}

anybody know what I should to do?

1

There are 1 answers

4
Mikko Viitala On BEST ANSWER

Hard to say because I can't access Lighthouse but try the following (notice how auth header is set).

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();

In this case your Authorization header looks like "Basic eW91ciB1c2VybmFtZTp5b3VyIEFQSSBrZXk=".