Comments (1)
Hi @nlogozzo
Sorry for the late reply, I didn't have notifications on for this repo 😢. This is untested, but should get you in the right direction. I'll add it to the README too.
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 Scratch {
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));
}
}
from tesla-api-client.
Related Issues (7)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tesla-api-client.