Giter VIP home page Giter VIP logo

tesla-api-client's Introduction

tesla-api-client

Java library for interfacing with the Tesla Owner API.

This library is generated using swagger-codegen. See swagger.yml to view the Swagger document used to generate the library.

Building Library from Swagger

  1. If not already installed, install or obtain a release of swagger-codegen If on Mac and using Homebrew, you can use brew install swagger-codegen
  2. Generate the library
    swagger-codegen generate -i swagger.yml -c config.json
    This will generate the library.
  3. Build the library
    gradle assemble # or `chmod +x ./gradlew && ./gradlew assemble`
  4. The library will be built in build/libs/

Installation

Gradle

compile 'com.github.jonahwh:tesla-api-client:2.3.1'

Maven

<dependency>
    <groupId>com.github.jonahwh</groupId>
    <artifactId>tesla-api-client</artifactId>
    <version>2.3.1</version>
</dependency>

Usage

Here is an example of how this library can be used to make requests to the Tesla API.

import com.github.jonahwh.ApiClient;
import com.github.jonahwh.tesla_api_client.AuthenticationApi;
import com.github.jonahwh.tesla_api_client.VehicleCommandsApi;
import com.github.jonahwh.tesla_api_client.VehiclesApi;
import com.github.jonahwh.tesla_api_client.model.CreateAccessTokenRequest;
import com.github.jonahwh.tesla_api_client.model.CreateAccessTokenResponse;
import com.github.jonahwh.tesla_api_client.model.GetVehiclesResponse;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

class TeslaApiClientExample {
    public static void main(String[] args) throws IOException {
        // Anonymous API Client for Authentication
        ApiClient anonymousApiClient = new ApiClient();
        anonymousApiClient.createDefaultAdapter();
        anonymousApiClient.configureFromOkclient(new OkHttpClient.Builder().build());

        // Authenticated API Client
        ApiClient authedApiClient = new ApiClient();
        authedApiClient.createDefaultAdapter();
        authedApiClient.configureFromOkclient(
                new OkHttpClient.Builder().addInterceptor(new AuthenticationInterceptor()).build()
        );

        AuthenticationApi authApi = anonymousApiClient.createService(AuthenticationApi.class);
        VehiclesApi vehiclesApi = authedApiClient.createService(VehiclesApi.class);
        VehicleCommandsApi commandsApi = authedApiClient.createService(VehicleCommandsApi.class);

        // TESLA_CLIENT_ID and TESLA_CLIENT_SECRET are available here: https://pastebin.com/YiLPDggh
        CreateAccessTokenRequest request = new CreateAccessTokenRequest()
                .grantType("password") // Do not change
                .clientId("TESLA_CLIENT_ID")
                .clientSecret("TESLA_CLIENT_SECRET")
                .email("[email protected]") // Tesla API User's email
                .password("password123"); // Tesla API User's password


        CreateAccessTokenResponse body = authApi.createOauthToken(request).execute().body();
        if (body != null) {
            // Tesla API Access Token for this account. You'll need to make this available to the
            // AuthenticationInterceptor
            String accessToken = body.getAccessToken();

            // Get the list of vehicles in the user's account
            GetVehiclesResponse vehicles = vehiclesApi.getVehicles().execute().body();
            if (vehicles != null) {
                // Get the ID of the first vehicle in the user's account.
                String vehicleId = vehicles.getResponse().get(0).getVehicleId();

                // Flash that vehicle's headlights
                commandsApi.flashLights(vehicleId);
            }
        }
    }
}

class AuthenticationInterceptor implements Interceptor {
    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) {
        Request.Builder builder = chain.request().newBuilder();

        // TODO: Get accessToken from CreateAccessTokenResponse here
        String accessToken = "";

        builder.addHeader("Authorization", String.format("Bearer %s", accessToken));
    }
}

tesla-api-client's People

Contributors

dependabot[bot] avatar jonahwh avatar kurthuwig avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

tesla-api-client's Issues

Q: Is there a client app using this?

I see this library was updated as recently as 4 days ago-- it looks sweet-- is there an OS Android Tesla app in progress somewhere that uses tesla-api-client??

There have been a few attempts to redesign the app, just wondering if anyone started actually doing it.

I should add there are a lot of use cases for a simple alternative app, one being the ability to use it on Chromebooks.

NSwag Studio Error: Unexpected initial token 'String' when populating object

Thanks for your work on this.
I just tried to use the most recent version with NSwag Studio and it errors out:

Newtonsoft.Json.JsonSerializationException: Unexpected initial token 'String' when populating object. Expected JSON object or array. Path 'definitions.VehicleState.properties.software_update.properties.version', line 1, position 45567.

Error seems to be here:

version: string

When I change this to the following it works:

          version: 
            type: string

Not submitting this as a PR as my knowledge of swagger is too limited to determine if this is an appropriate fix

Gradle: unable to use 1.12.0

I am unable to use version 1.12.0 of this api through Gradle as it says depedency not found. If I change it to 1.11.0, I have no issues and it downloads successfully. Haven't tried with maven as well

MFA implementation

Just a question; have you ever gotten the MFA version working? I see you use the Apache library, but AFAIK it uses the callback (which can't be set for Tesla). I'm struggling to get it running in my Java API implementation.

Authentication not working at all?

Looks like the authentication scheme has changed completely? I've tried to adapt the snippet provided in the readme but the authorization request fails with a 400. According to this website, what is currently implemented is only step 4 albeit with a wrong set of request parameters. :/

I've tried chaning the grant_type to authorization_code, but that then leads to a 401. That at least indicates an authorization attempt but generally, this feels not working at all.

Help Getting Started

I was able to add this api to my java project however, i'm unable to figure out how to authenticate a Tesla user account with an email and password to be able to get a vehicle and start doing some commands on the vehicle. Most Tesla apis have a getting started or usage section and this one does not. Can someone provide me with example code on how to use this api?

docs/AuthenticationApi.md is outdated

Hallo, the documentation is a little bit outdated, because the docs/AuthenticationApi.md did not work with the last version anymore.

CreateAccessTokenResponse result = apiInstance.createOauthToken(body);
can not be casted.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.