JSkimming / tesla-net

A .NET client library for the Tesla Owner API
https://tesla-api.timdorr.com/
Apache License 2.0
35 stars 9 forks source link

Usage examples #18

Open ramonsmits opened 5 years ago

ramonsmits commented 5 years ago

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.

            var result = await client.GetVehiclesAsync(TESLA_ACCESS_TOKEN);
            var vehicles = result.Data.Response;
            foreach (var vehicle in vehicles)
            {
                Console.WriteLine(vehicle.DisplayName);
            }
ramonsmits commented 5 years ago

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);
chrispi68 commented 5 years ago

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.

adamncsu commented 5 years ago

Some basic documentation would be nice.

Gruski commented 5 years ago

I agree we need some documentation.

nlogozzo commented 5 years ago

If someone has figured it out, can you comment an example to get started

adamncsu commented 5 years ago

@nlogozzo Here you go:

const string TESLA_CLIENT_ID = "81527cff06843c8634fdc09e8ac0abefb46ac849f38fe1e431c2ef2106796384";
const string TESLA_CLIENT_SECRET = "c7257eb71a564034f9419ee651c7d0e5f7aa6bfbd18bafb5c5c033b093bb2fa3";
const string EMAIL = "your@email.com";
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

nlogozzo commented 5 years ago

@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?

adamncsu commented 5 years ago

@nlogozzo I don’t think it matters. Its just a descriptor for the API to know who’s calling it.

nlogozzo commented 5 years ago

@adamncsu Okay. Thank You!

nlogozzo commented 5 years ago

@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?

adamncsu commented 5 years ago

@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.

JSkimming commented 5 years ago

@adamncsu Nope, I've only added the APIs I needed, though I'm happy to take PRs :-)

ramonsmits commented 4 years ago

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?

@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.

ramonsmits commented 4 years ago

To extend @adamncsu his sample, here one that set the bearer token once. The HttpClient should be reused as much as possible as this will reuse an open connection to the web server.

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");