-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Usage examples #18
Comments
Ok it seems I found how to use it: var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("MySuperClient/0.1");
var teslaClient = new TeslaClient(TeslaClientBase.DefaultBaseUri, httpClient); |
I still don't know how to log in, how to access all the vehicles in my account or how to turn on the heating. |
Some basic documentation would be nice. |
I agree we need some documentation. |
If someone has figured it out, can you comment an example to get started |
@nlogozzo Here you go: const string TESLA_CLIENT_ID = "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384";
const string TESLA_CLIENT_SECRET = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3";
const string EMAIL = "[email protected]";
const string PASSWORD = "yourpassword";
// authenticate
string accessToken = null;
using (var httpClient = new HttpClient(TeslaClientBase.CreateDefaultHttpClientHandler(), false))
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("tesla-net/0.6.2");
using (var auth = new TeslaAuthClient(httpClient))
{
var authResult = auth.RequestAccessTokenAsync(TESLA_CLIENT_ID, TESLA_CLIENT_SECRET, EMAIL, PASSWORD).GetAwaiter().GetResult();
accessToken = authResult.Data.AccessToken;
}
}
// make api calls
using (var httpClient = new HttpClient(TeslaClientBase.CreateDefaultHttpClientHandler(), false))
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("tesla-net/0.6.2");
using (var api = new TeslaClient(httpClient))
{
// get vehicle list
var vehiclesResult = api.GetVehiclesAsync(accessToken).GetAwaiter().GetResult();
var vehicles = vehiclesResult.Data.Response;
// check charge status
var chargeResult = api.GetChargeStateAsync(vehicles.First().Id, accessToken).GetAwaiter().GetResult();
var chargeState = chargeResult.Data.Response;
Console.WriteLine($"Charge state: {chargeState.ChargingState}");
Console.WriteLine($"Battery level: {chargeState.BatteryLevel}%");
Console.WriteLine($"Estimated range: {chargeState.BatteryRange} mi");
}
} The client ID and secret key are apparently subject to change, but I'm not sure how often. You can find this information here: https://tesla-api.timdorr.com/api-basics/authentication |
@adamncsu What is the "tesla-net/0.6.2"? Should that be the version of the api i am using, in my case 0.7.0? |
@nlogozzo I don’t think it matters. Its just a descriptor for the API to know who’s calling it. |
@adamncsu Okay. Thank You! |
@adamncsu Sorry to bother, I was able to get the basic api setup and able to see some basic vehicle info. However, I can't figure out how to do commands, such as unlock the doors, increase the volume, etc. Can you help? |
@nlogozzo I don’t think those commands are covered in the scope of this project, but may e @JSkimming could answer. I don’t see it in the code. |
@adamncsu Nope, I've only added the APIs I needed, though I'm happy to take PRs :-) |
@adamncsu @nlogozzo That is YOUR application identification according to the user-agent spec. So do not use something like "TeslaNET". This is for the server so understand what kind of application is connecting. |
To extend @adamncsu his sample, here one that set the bearer token once. The var httpClient = new HttpClient(TeslaClientBase.CreateDefaultHttpClientHandler(), false);
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
// authenticate
var auth = new TeslaAuthClient(httpClient);
GC.SuppressFinalize(auth); // Do NOT dispose, this wil also dispose the httpclient. Is a bug in current version!
var authResult = auth.RequestAccessTokenAsync(TESLA_CLIENT_ID, TESLA_CLIENT_SECRET, EMAIL, PASSWORD).GetAwaiter().GetResult();
// Set Bearer token in default authorization header
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.Data.AccessToken);
var api = new TeslaClient(httpClient); // Store globally if using a single account
GC.SuppressFinalize(api); // Do NOT dispose, this wil also dispose the HttpClient. Is a bug in current version!
// get vehicle list
var vehiclesResult = api.GetVehiclesAsync().GetAwaiter().GetResult();
var vehicles = vehiclesResult.Data.Response;
// check charge status
var chargeResult = api.GetChargeStateAsync(vehicles.First().Id).GetAwaiter().GetResult();
var chargeState = chargeResult.Data.Response;
const double MilesToKilometersFactor = 1.609344; // I live in Europe, we use the metric system :-)
Console.WriteLine($"Charge state: {chargeState.ChargingState}");
Console.WriteLine($"Battery level: {chargeState.BatteryLevel}%");
Console.WriteLine($"Estimated range: {chargeState.BatteryRange/MilesToKilometersFactor} km"); |
Please extend the readme with atleast a simple example explaining how to use the framework. I'm unable to use it.
For example, I tried the following but it doesnt work. Maybe my access token is incorrect but it is also unclear how to obtain an access token in the first place.
The text was updated successfully, but these errors were encountered: