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




        }

    }
} `
great77
  • 143
  • 4
  • 20
  • 1
    Where's the page of the documentation of the API? I couldn't find it online. Only I could find was this: https://secure.logmein.com/ws/api/alertEventGetList.aspx?help=1. You are sending them in the header, but in the link above, it appears the values goes in the querystring. Did you try that? Also, why don't you use the Httpclient? – jpgrassi Jan 21 '16 at 11:17
  • Thank you jpprassi. They have two API. The one you wrote, is fine for me . The one am using is this to get hardware inventory /software inventory. The pdf file is this https://secure.logmein.com/welcome/documentation/EN/pdf/LogMeInCentralPublicAPIReference.pdf . Please let me know your thought. I have used autoit and it seems is given me expires null token null. I want to c#. I have am not expert yet in c#, but just want to know if this can resolve my issue – great77 Jan 21 '16 at 11:21

1 Answers1

2

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

Community
  • 1
  • 1
jpgrassi
  • 5,482
  • 2
  • 36
  • 55
  • Thanks jpgrassi. It is giving error. How do I include the httpclient, am trying to use this alternative if it will help. the documentation of api in this link https://secure.logmein.com/ws/api/alertEventGetDetails.aspx?help=1 – great77 Jan 21 '16 at 13:07
  • Thanks jpgrassi. It is giving error. How do I include the httpclient, am trying to use this alternative if it will help. the documentation of api in this link https://secure.logmein.com/ws/api/alertEventGetDetails.aspx?help=1 . I have it giving response as only OK, how can I write the intlist in this part of the page . the code I have sURL = "https://secure.logmein.com/ws/api/alertEventGetDetails.aspx?companyid=1xxxxxx&psk=xxxxxxx&alerteventidlist=1,2, 3, 10,12" . I thought maybe there is a way to do this, and this is the only one I – great77 Jan 21 '16 at 13:13
  • I've updated my answer to include reference to the HttpClient, see there. This is a new API you're talking about. My answer was for the one you post in your question (reports), which uses a totally different authentication process. Which one are you really using? – jpgrassi Jan 21 '16 at 13:14
  • It gives this error Error 1 The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?) I have included the using System.Net.Http.HttpClient . I trying the other APi to see if that will give me better solution. – great77 Jan 21 '16 at 15:07
  • You need to add reference to System.Net.Http (sorry, I typed the wrong namespace. Fixed now). Then add a using in your .cs file to using System.Net.Http; – jpgrassi Jan 21 '16 at 16:09
  • I used using System.Net.http at the top of the code. How do i add reference because the error says "The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?). Sorry am using c# not very much familiar. – great77 Jan 21 '16 at 17:40
  • 2
    First you need to add the reference into your project solution. Right click in References -> Add Reference, Look for System.Net.Http and click Add. After that, you include the using at the top of your file. https://msdn.microsoft.com/en-us/library/wkze6zky.aspx – jpgrassi Jan 21 '16 at 17:44
  • Thank you so much. The program has no error again and says it exited with code 0, however how can I display the result on console window . used Console.Writeline (response). can not see any thing coming out as output. Thanks again – great77 Jan 21 '16 at 18:40
  • I have been able to view the output using Console.Read(). it is responding to the headers and says status code : 401 and reason phrase unauthorized. – great77 Jan 21 '16 at 18:45
  • Decided to use Restsharp previous one is gives aunauthorized, the the code for restsharp, but the output is only restsharp.restresponse.see below var ade = new RestClient("https://secure.logmein.com/"); var request = new RestRequest("public-api/v1/inventory/hardware/reports", Method.GET); request.AddHeader("authorization", "{\"companyId\":1143,\"psk\":\"00_lotn3khqxkwbz8w00t078gggphj\"}"); request.AddHeader("accept", "application/Json; charset=utf-8"); IRestResponse response = ade.Execute(request); – great77 Jan 22 '16 at 10:52
  • I am pleased to tell you that the script is now connected and working based on RESTAPI which gives the same output as AUTOIT I have null token and expires token. LogmeIN webpage server is not pulling report for the inventory report, I will post another question about the other API, if that one can pull the inventory report. Cheers. – great77 Jan 22 '16 at 11:18
  • Thanks. The problem with this API is that I can not pull the info because it says token null expires null. I have been contacting Logmein and they said is not their server. I am looking at using other api webpage. The two working script that I have autoit gives exact same result. – great77 Jan 22 '16 at 11:36