public TeslaAuthHelper(string userAgent, TeslaAccountRegion region = TeslaAccountRegion.Unknown) { UserAgent = userAgent; loginInfo = new LoginInfo { CodeVerifier = RandomString(86), State = RandomString(20) }; client = CreateHttpClient(region); }
/// <summary> /// Should your Owner API token begin with "cn-" you should POST to auth.tesla.cn Tesla SSO service to have it refresh. Owner API tokens /// starting with "qts-" are to be refreshed using auth.tesla.com /// </summary> /// <param name="region">Which Tesla server is this account created with?</param> /// <returns>Address like "https://auth.tesla.com", no trailing slash</returns> static string GetBaseAddressForRegion(TeslaAccountRegion region) { switch (region) { case TeslaAccountRegion.Unknown: case TeslaAccountRegion.USA: return("https://auth.tesla.com"); case TeslaAccountRegion.China: return("https://auth.tesla.cn"); default: throw new NotImplementedException("Fell threw switch in GetBaseAddressForRegion for " + region); } }
HttpClient CreateHttpClient(TeslaAccountRegion region) { var ch = new HttpClientHandler { CookieContainer = new CookieContainer(), AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, AllowAutoRedirect = false, UseCookies = true }; var client = new HttpClient(ch) { BaseAddress = new Uri(GetBaseAddressForRegion(region)), DefaultRequestHeaders = { ConnectionClose = false, Accept = { new MediaTypeWithQualityHeaderValue("application/json") }, } }; client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent); return(client); }