public JlrSharpConnection(UserDetails userDetails, TokenStore tokenStore) { _userInfo = userDetails; _tokens = tokenStore; // We will need this token later on for re-generating the tokens _oAuth = new OAuth { ["grant_type"] = "refresh_token", ["refresh_token"] = tokenStore.refresh_token }; ConnectWithExistingCreds(); }
/// <summary> /// Connects and retrieve the auth token, which is required for future operations /// </summary> private void Connect(OAuth oAuthToken) { Trace.TraceInformation($"Connecting device ID \"{_userInfo.DeviceId}\""); // Add some default headers _authClient.AddDefaultHeader("X-Device-Id", _userInfo.DeviceId.ToString()); _authClient.AddDefaultHeader("Connection", "close"); _deviceClient.AddDefaultHeader("X-Device-Id", _userInfo.DeviceId.ToString()); _deviceClient.AddDefaultHeader("Connection", "close"); _vehicleClient.AddDefaultHeader("X-Device-Id", _userInfo.DeviceId.ToString()); _vehicleClient.AddDefaultHeader("Connection", "close"); // Authenticate RestRequest loginRequest = new RestRequest("tokens", Method.POST, DataFormat.Json); loginRequest.AddHeader("Authorization", "Basic YXM6YXNwYXNz"); loginRequest.AddJsonBody(oAuthToken); IRestResponse <TokenStore> response = _authClient.Execute <TokenStore>(loginRequest); if (!response.IsSuccessful) { throw new AuthenticationException("Error authenticating with OAuth Token"); } _tokens = response.Data; Trace.TraceInformation("Authentication complete"); // Configure the access tokens for connections _deviceClient.AddDefaultHeader("Authorization", $"Bearer {_tokens.access_token}"); _vehicleClient.AddDefaultHeader("Authorization", $"Bearer {_tokens.access_token}"); // Register device RegisterDevice(); // Log in the user LoginUser(); // Grab all associated vehicles GetVehicles(); //#if DEBUG // DumpData(); //#endif }