This object is in archive! 

Cant get api for login to work in c#

Andrej shared this question 10 years ago
Need Answer

Hi


I am trying login with api from my c3 project.

First i call


  1. WebClient client = new WebClient();
  2. string url = "https://my.zipato.com:443/zipato-web/v2/user/init";
  3. var result = JsonConvert.DeserializeObject<init>(client.DownloadString(url));


from here i get valid response with jsessionid and nonce.

Then i make a token from it:


  1. var token = tools.CalculateSha1(nonce + tools.CalculateSha1(password, Encoding.Default), Encoding.Default);

pasword is my password for login and nonce i get from init api.

Ok now i have a token and then i try this:


  1. var url22 = "https://my.zipato.com:443/zipato-web/v2/user/login";
  2. string username = "username";
  3. WebClient client2 = new WebClient();
  4. string url2 = url22 + "?username=" + username + "&token=" + token.ToString();
  5. var result2 = JsonConvert.DeserializeObject(client.DownloadString(url2));


But i am always getting success:false, user not found or wrong password.

I read that i need to send cookie with session id but i dont know how :).

Anyone has an ideo on hot to login with c#?

Replies (9)

photo
1

Hi Andrej,


Have you solved your problem? Because i'm facing the same.


-Felix

photo
1

Either you are hashing wrong, or you are missing an argument on the authenticate call. I pass &method=SHA1 as well.


  1. using System;
  2. using System.IO;
  3. using System.Net.Http;
  4. using System.Net.Http.Headers;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using Newtonsoft.Json;
  8. namespace ZipaSharp
  9. {
  10. public class ZipaSharpAPI
  11. {
  12. private string _apiEndpoint = string.Empty;
  13. private string _nonce = string.Empty;
  14. private string _jsessionid = string.Empty;
  15. public ZipaSharpAPI(string apiEndpoint, string username, string password)
  16. {
  17. _apiEndpoint = apiEndpoint;
  18. if (!_apiEndpoint.EndsWith("/"))
  19. _apiEndpoint += "/";
  20. var session = JsonGet("user/init");
  21. if (session["success"] == "true")
  22. {
  23. _nonce = session["nonce"];
  24. _jsessionid = session["jsessionid"];
  25. AuthenticateUser(username, password);
  26. }
  27. }
  28. #region API calls
  29. public void GetDevices()
  30. {
  31. }
  32. #endregion
  33. #region "Private methods"
  34. private void AuthenticateUser(string username, string password)
  35. {
  36. var token = SHA1(_nonce + SHA1(password));
  37. var loginResponse = JsonGet(string.Format("user/login?username={0}&token={1}&method=SHA1", username, token));
  38. if (loginResponse["success"] != "true")
  39. throw new ArgumentException("Unable to authenticate with Zipabox API. Wrong username and/or password.");
  40. }
  41. private string SHA1(string text)
  42. {
  43. // convert string to stream
  44. var byteArray = Encoding.ASCII.GetBytes(text);
  45. var stream = new MemoryStream(byteArray);
  46. using (var bs = new BufferedStream(stream))
  47. {
  48. using (var sha1 = new SHA1Managed())
  49. {
  50. var hash = sha1.ComputeHash(bs);
  51. var formatted = new StringBuilder(2*hash.Length);
  52. foreach (byte b in hash)
  53. {
  54. formatted.AppendFormat("{0:X2}", b);
  55. }
  56. return formatted.ToString().ToLower();
  57. }
  58. }
  59. }
  60. public dynamic JsonGet(string methodAndArguments)
  61. {
  62. var strJson = MakeGetRequest(_apiEndpoint + methodAndArguments);
  63. dynamic jObj = JsonConvert.DeserializeObject(strJson);
  64. return jObj;
  65. }
  66. private string MakeGetRequest(string requestUrl)
  67. {
  68. using (var handler = new HttpClientHandler() { UseCookies = false})
  69. {
  70. using (var client = new HttpClient(handler))
  71. {
  72. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  73. var message = new HttpRequestMessage(HttpMethod.Get, requestUrl);
  74. if (!string.IsNullOrEmpty(_jsessionid))
  75. {
  76. message.Headers.Add("Cookie", "JSESSIONID=" + _jsessionid);
  77. }
  78. var response = client.SendAsync(message).Result;
  79. return response.Content.ReadAsStringAsync().Result;
  80. }
  81. }
  82. }
  83. #endregion
  84. }
  85. }

photo
1

Thank you so much Pål, the problem was in the hashing.

photo
2

Hi guys,


I made an open source C# library which is on GitHub and NuGet. Feel free to help. https://github.com/leonmeijer/Zipato.NET

photo
1

Hi!

Great job leonMeijer! Thanks you very much. I was planning a development of an application on Windows Phone. Your work is going to save me a lot of time!


Question : How get allendpoints of type Switch On/Off?

photo
1

OK, we can use endpointtype to filtre actuator.onoff (or actuator.onoff.light)

photo
1

Hey chHome. It's also my intention to build an Universal (Win 8.1/WinPhone 8.1) or UWP (Universal Windows Platform, Windows 10) app. It will be a hobby project, but perhaps we can join our efforts.


Regarding your question, I'm working right now on extending the library with support for retrieving devices, better attribute support and I will add a method to get all on/off switches.

photo
1

I checked in to Github. You can call


  1. var onOffEndpoints = await client.GetEndpointsWithOnOffAsync();


I didn't find endpointtype reliable enough so I'm retrieving all endpoints and all attributes and I link these two together in the object model. Then I return a list of all endpoints that have an attribute with a definition of com.zipato.cluster.OnOff. By default I exclude the Zipabox on/off commands (for example for the LEDs). And by default hidden devices are returned, unless you pass hideHidden = true which makes a call for each endpoint unfortunately.

photo
2

Hi,


try this one:


https://github.com/ggruner/Zipatoapi


Gruß Helle

Leave a Comment
 
Attach a file