-
-
Save hsakoh/cc079f56b509d601db6924ed70d5b8b9 to your computer and use it in GitHub Desktop.
SwitchBot Lock Key SRP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Text; | |
using System.Text.Encodings.Web; | |
using System.Text.Json; | |
using System.Threading.Tasks; | |
namespace SwitchBotAPITest3 | |
{ | |
internal class Program | |
{ | |
static async Task Main() | |
{ | |
var httpClient = new HttpClient(new HttpClientHandler() | |
{ | |
//Proxy = new WebProxy("localhost", 8888) | |
}); | |
var clientId = "5nnwmhmsa9xxskm14hd85lm9bm"; | |
var userName = "<your account mailaddress>"; | |
var userPassword = "<your account password>"; | |
var deviceMac = "<your lock device mac without Colon and UpperCase>"; | |
LoginResp loginResp; | |
{ | |
var json = JsonSerializer.Serialize(new | |
{ | |
clientId = clientId, | |
username = userName, | |
password = userPassword, | |
grantType = "password", | |
verifyCode = "", | |
}, DefaultJsonSerializerOption); | |
var response = await httpClient.PostAsync("https://account.api.switchbot.net/account/api/v1/user/login" | |
, new StringContent(json, Encoding.UTF8, "application/json1")); | |
response.EnsureSuccessStatusCode(); | |
var responseString = await response.Content.ReadAsStringAsync(); | |
loginResp = JsonSerializer.Deserialize<LoginResp>(responseString)!; | |
} | |
CommunicateResp communicateResp; | |
{ | |
var json = JsonSerializer.Serialize(new | |
{ | |
device_mac = deviceMac, | |
keyType = "user", | |
}, DefaultJsonSerializerOption); | |
var requestMessage = new HttpRequestMessage | |
{ | |
Method = HttpMethod.Post, | |
Content = new StringContent(json, Encoding.UTF8, "application/json"), | |
RequestUri = new Uri("https://wonderlabs.us.api.switchbot.net/wonder/keys/v1/communicate") | |
//RequestUri = new Uri("https://wonderlabs.ap.api.switchbot.net/wonder/keys/v1/communicate") | |
//RequestUri = new Uri("https://wonderlabs.eu.api.switchbot.net/wonder/keys/v1/communicate") | |
}; | |
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(loginResp.body.access_token); | |
var response = await httpClient.SendAsync(requestMessage); | |
response.EnsureSuccessStatusCode(); | |
var responseString = await response.Content.ReadAsStringAsync(); | |
communicateResp = JsonSerializer.Deserialize<CommunicateResp>(responseString)!; | |
} | |
Console.WriteLine($"statusCode:{communicateResp.statusCode},message:{communicateResp.message}"); | |
Console.WriteLine($"keyId:{communicateResp.body.communicationKey.keyId},key:{communicateResp.body.communicationKey.key}"); | |
} | |
static JsonSerializerOptions DefaultJsonSerializerOption = new JsonSerializerOptions() | |
{ | |
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping | |
}; | |
public class LoginResp | |
{ | |
public int statusCode { get; set; } | |
public string message { get; set; } | |
public Body body { get; set; } | |
public class Body | |
{ | |
public string access_token { get; set; } | |
public int expires_in { get; set; } | |
public int refresh_expires_in { get; set; } | |
public string refresh_token { get; set; } | |
public string token_type { get; set; } | |
} | |
} | |
public class CommunicateResp | |
{ | |
public int statusCode { get; set; } | |
public Body body { get; set; } = default!; | |
public string message { get; set; } = default!; | |
public class Body | |
{ | |
public Communicationkey communicationKey { get; set; } = default!; | |
public class Communicationkey | |
{ | |
public string keyId { get; set; } = default!; | |
public string keyType { get; set; } = default!; | |
public long createTime { get; set; } | |
public string key { get; set; } = default!; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment