private async Task <CommandRequestStatus> Login(DeviceAuth authObject) { var loginToken = new JwtBuilder() .WithAlgorithm(new HMACSHA256Algorithm()) .AddClaims(authObject) .WithSecret(JwtSecretKey) .Build(); logger("Logging in..."); var result = await client.PostAsync("https://api.gm.com/api/v1/oauth/token", new StringContent(loginToken)); var loginResponse = await result.Content.ReadAsStringAsync(); logger($"Response Token: {loginResponse}"); if (loginResponse.Contains("error")) { var loginErrorResponse = serializer.Deserialize <LoginError>(loginResponse); return(new CommandRequestStatus() { Successful = false, ErrorMessage = loginErrorResponse.error }); } logger(decoder.Decode(loginResponse, JwtSecretKey, verify: true)); var json = decoder.DecodeToObject <LoginReply>(loginResponse, JwtSecretKey, verify: true); AddAccessToken(json); return(new CommandRequestStatus() { Successful = true }); }
public static JwtBuilder AddClaims(this JwtBuilder builder, DeviceAuth deviceAuth) { return(builder .AddClaim("client_id", deviceAuth.client_id) .AddClaim("device_id", deviceAuth.device_id) .AddClaim("username", deviceAuth.username) .AddClaim("password", deviceAuth.password) .AddClaim("nonce", deviceAuth.nonce) .AddClaim("timestamp", deviceAuth.timestamp) .AddClaim("scope", deviceAuth.scope) .AddClaim("grant_type", deviceAuth.grant_type)); }
private async Task <CommandRequestStatus <DetailedVehialInfo> > LogInAndGetDetailedVehicalInfo() { var authObject = new DeviceAuth() { client_id = ClientId, device_id = DeviceId, grant_type = "password", username = username, password = password, }; CreateHttClient(); var loginResponse = await Login(authObject); if (loginResponse.Successful == false) { return(CommandRequestStatus <DetailedVehialInfo> .GetFailed(loginResponse.ErrorMessage)); } logger("Getting vehicals..."); var getResponse = await client.GetAsync("https://api.gm.com/api/v1/account/vehicles"); var responseBody = await getResponse.Content.ReadAsStringAsync(); var vehicals = serializer.Deserialize <VehicalList>(responseBody); var firstCar = vehicals.vehicles.vehicle.First(); logger($"Getting detailed info on {firstCar.vin}"); var vehicalDetailsReply = await client.GetAsync($"https://api.gm.com/api/v1/account/vehicles/{firstCar.vin}?includeCommands=true&includeFeatures=true&includeDealers=true&includeCarrierAccount=true"); var vehicalDetailsString = await vehicalDetailsReply.Content.ReadAsStringAsync(); var detailedVehicalInfo = serializer.Deserialize <DetailedVehialInfo>(vehicalDetailsString); foreach (var item in detailedVehicalInfo.vehicle.commands.command) { logger(item.name); logger(item.description); } return(CommandRequestStatus <DetailedVehialInfo> .GetSuccessful(detailedVehicalInfo)); }