Search code examples
c#apilogmein

C# 400 bad request and long characters how api important


using System;
using System.Net;
using System.IO;

namespace MakeAGETRequest_charp
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class Class1
    {
         static void Main(string[] args)
       {
           string sURL;


           sURL = "https://secure.logmein.com/public-api/v1/inventory/hardware/reports";

            WebRequest wrGETURL;
            wrGETURL = WebRequest.Create(sURL);
            wrGETURL.UseDefaultCredentials = true;

            wrGETURL.PreAuthenticate = true;
            wrGETURL.Credentials = CredentialCache.DefaultCredentials;
            wrGETURL.ContentType = "application/json";

             //wrGETURL.Headers.Add("Company Id:", "xxxxxxxxx");
           // wrGETURL.Headers.Add("PSK:", "0x_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            wrGETURL.Headers[HttpRequestHeader.Authorization] = "companyId":"114xxxxxx", "psk":" 0x_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";




            WebProxy myProxy = new WebProxy("myproxy", 80);
            myProxy.BypassProxyOnLocal = true;

           // wrGETURL.Proxy = WebProxy.GetDefaultProxy();  // obsolete not needed

            Stream objStream;
            objStream = wrGETURL.GetResponse().GetResponseStream();

            StreamReader objReader = new StreamReader(objStream);

            string sLine = "";
            int i = 0;

            while (sLine != null)
            {
                i++;
                sLine = objReader.ReadLine();
                if (sLine != null)
                    Console.WriteLine("{0}:{1}", i, sLine);
            }
            Console.ReadLine();
        }
    }
}

I am trying to pull a report from Logmein website and based on their documentation, I really need help first with this issue to get hardware inventory.... the error is 400 bad request and long characters

How can I get it authenticated by the psk and company id?

Working Code now using RESTAPI and getting similar result with my AUTOit SCRIPT is null token and expires token. The problem with LOgmein server, not giving report : `using System;

using System.Net; using System.IO; using System.Net.Http; using RestSharp;

namespace MakeAGETRequest_charp { /// /// Summary description for Class1. /// public class ConsoleApplication3 {

    static void Main(string[] args)

    {

        var ade = new RestClient("https://secure.logmein.com/");
        var request = new RestRequest("public-api/v1/inventory/system/reports", Method.GET);
  request.AddHeader("authorization", "{\"companyId\":113,\"psk\":\"00_loqxkwbz8w0xxxxxxx\"}");
    request.AddHeader("accept", "application/Json; charset=utf-8");
   IRestResponse response = ade.Execute(request);


        //string sURL;


   var result = "";


       result = response.Content;
       Console.Write(result);
       Console.Read()


   //Console.WriteLine(ade.Execute(request));
           // Console.Read();




        }

    }
} `

Solution

  • Since I don't have the parameters to actually log in into the API, I can't say for sure this will work. But it does implement all the requirements specified in the docs you referenced

    I'm doing this example using the HttpClient class which resides inside of the System.Net.Http assembly.

    using(var client = new HttpClient())
    {
        //Base Url
        client.BaseAddress = new Uri("http://secure.logmein.com/public-api/v1/");
    
        //Add mandatory Request Headers General parameters Section of doc               
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
        client.DefaultRequestHeaders.Add("Accept", "application/json; charset=utf-8");
    
        //Authentication header. 
        string auth = "{\"companyId\":123456,\"psk\":\"ABCZSWDED\"}";
        client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization:", auth);
    
        HttpResponseMessage response = client.GetAsync("inventory/hardware/reports").Result;
    }
    

    The only problem is that the API expects an Authorization header with a JSON value. This is not the "default" method of Authorization. Because of this, you need to use TryAddWithoutValidation to skip validation and add the json in the request header.

    If this still doesn't work, you'll probably need to use another http client to make the request. I've seen people using RestSharp to log in into this API with success. See this similar question