0

I am running into an issue with authentication for the LogMeIn api. The authorization value is a JSON object. When running my code, I run into FormatException error.

"A first chance exception of type 'System.FormatException' occurred in System.Net.Http.dll Additional information: The format of value '{"companyId":9999999,"psk":"o2ujoifjau3ijawfoij3lkas3l2"}' is invalid."

        var client = new HttpClient();
        client.BaseAddress = new Uri("http://secure.logmein.com/public-api/v1/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Add("Accept", "application/JSON; charset=utf-8");

        string s = "{\"companyId\":9999999,\"psk\":\"o2ujoifjau3ijawfoij3lkas3l2\"}";

        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(s);

       HttpResponseMessage response = client.GetAsync("authentication").Result;

How should I be formatting the authorization key in this situation?

Aero Chocolate
  • 1,477
  • 6
  • 23
  • 39

2 Answers2

2

This happens, because LogMeIn does not use standard authentication schema like "Basic". You should add message header without validation:

string s = "{\"companyId\":9999999,\"psk\":\"o2ujoifjau3ijawfoij3lkas3l2\"}";
string h = "Authorization";

client.DefaultRequestHeaders.TryAddWithoutValidation(h, s);

See here and here

You can check that request you send have correct header using tool (web debugger) called Fiddler (It's a must have tool for web developer). You need to add the following configuration into web.config in order to route http traffic through Fiddler proxy:

<system.net>
   <defaultProxy>
       <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
   </defaultProxy>
</system.net>

or specify it in a request intself:

client.Proxy = new Uri("http://localhost:8888/"); // default address of fiddler
Community
  • 1
  • 1
Vova
  • 1,356
  • 1
  • 12
  • 26
  • It seems also that you can provide company Id and PSK in url parameters: – Vova Oct 02 '15 at 04:56
  • Thanks for the reply! The header according to the guide seems to be correct. I tried what you had written there and it did not throw the exception. https://secure.logmein.com/welcome/documentation/EN/pdf/LogMeInCentralPublicAPIReference.pdf. However, my authentication seems to fail due to the string. I also tried client.DefaultRequestHeaders.Add("Authorization",s). But the the System.FormatException comes up again. – Aero Chocolate Oct 02 '15 at 08:16
  • Try to use TryAddWithoutValidation method (see updated answer) – Vova Oct 02 '15 at 11:42
  • The authorization fails but at least I am no longer receiving the formatting issue. I am trying to find examples where JSON is passed in as a header. – Aero Chocolate Oct 02 '15 at 23:49
  • Can you check if Authorization header looks exactly the same like in a documentation? – Vova Oct 03 '15 at 00:02
  • Yes. It is odd that the format isn't taking it. I tried using RestSharp and it is working fine now. Still bugs me that the above doesn't work though. – Aero Chocolate Oct 03 '15 at 19:12
  • I checked both variants (HttpClient and RestSharp) they generate the same authorizaiton header except casing Authorization vs authorization. Difference is only in accept and some other headers. RestSharp generations a UserAgent header as well. – Vova Oct 05 '15 at 21:30
-1

I used RestSharp and was able to get it to authenticate.

        var request = new RestRequest(Method.GET);
        request.AddHeader("authorization", "{\"companyId\":9999999,\"psk\":\"o2ujoifjau3ijawfoij3lkas3l2\"}");
        request.AddHeader("accept", "application/Json; charset=utf-8c");
        IRestResponse response = client.Execute(request);

However, it still bugs me that I wasn't able to get it to work with the HttpClient.

Aero Chocolate
  • 1,477
  • 6
  • 23
  • 39